问题
In following functions, taken from LDD:
ssize_t read(struct file *filp, char __user *buff, size_t count, loff_t *offp);
ssize_t write(struct file *filp, const char __user *buff, size_t count, loff_t *offp);
Why there is the need of loff_t *offp
? Can't I use directly filp
to update f_pos
?
Moreover in page 54 the author says:
Read and write should update a position using the pointer they receive as the last argument instead of acting on
filp->f_pos
directly. The one exception to this...
OK, so it's better to use the offp
pointer, but why?
回答1:
filp->f_pos
is current pointer position in file, whereas offp
is where user does access to file. You advance file pointer on successful read/write operation, if you failed you should not change file pointer. Kernel does it itself, if you successfully did read/write it will change filp->f_pos
to offp
. Citing LDD3:
Whatever the amount of data the methods transfer, they should generally update the file position at *offp to represent the current file position after successful completion of the system call. The kernel then propagates the file position change back into the file structure when appropriate.
回答2:
One reason could be to support the pread, pwrite, preadv, pwritev syscalls, which take an offset as one of the arguments, and are specified to NOT update the file position. It seems cleaner to have the lower layer code implement something close to the p* functions, and then have wrappers that use filp->f_pos and update the position for the plain read/write/readv/writev syscalls.
来源:https://stackoverflow.com/questions/23501185/reason-why-use-loff-t-offp-instead-of-direct-filp-f-pos-usage