Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy struct file?

Tags:

linux-kernel

I'm porting old linux kernel code for newer version 2.6.32.

There is a part that copies a file descriptor. The idea was to allocate a new file descriptor and a new struct file and use them with another f_op and , leaving all other fields of struct file equivalent to original's.

How do I do this in a modern kernel? I've written an approximate implementation but I don't know whether i should call file_get, path_get or do others use counter incrementation.

struct file * copy_file(const struct file * iOrig, int * oNewFd) {
  if (!orig)
    return 0; 
  *oNewFd = get_unused_fd();
  if (*oNewFd < 0)
    return 0;
  struct file * rv = alloc_file(orig->f_path.mnt, orig->f_path.dentry, orig->f_path.mode, orig->f_op);
  if (!rv)
    goto free_fd;
  fd_install(fd, rv);
  return rv;
free_fd:
  put_unused_fd(*oNewFd)
  return 0;
}

P.S. In fact having all fileds of original file copied is not neccessary. I just need to allow a new set of file operations in user-space. So creating a new descriptor owned by current with a given f_op will do.

like image 877
Basilevs Avatar asked Aug 17 '10 11:08

Basilevs


People also ask

How do I copy a folder structure without files in Outlook?

The trick to copy the folder structure of your current mailbox is to archive the mailbox with a date so far in the past that none of your emails are actually being archived. This will create an archive file which contains all of your current folders but will all be empty as well.


1 Answers

path_get sounds fine. Check out an example here http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/fs/pipe.c#L1046 and you'll be able to find more refs there if you need them.

like image 113
ZXX Avatar answered Oct 13 '22 06:10

ZXX