2 * linux/fs/9p/vfs_file.c
4 * This file contians vfs file ops for 9P2000.
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
26 #include <linux/module.h>
27 #include <linux/errno.h>
29 #include <linux/sched.h>
30 #include <linux/file.h>
31 #include <linux/stat.h>
32 #include <linux/string.h>
33 #include <linux/inet.h>
34 #include <linux/list.h>
35 #include <linux/pagemap.h>
36 #include <linux/utsname.h>
37 #include <asm/uaccess.h>
38 #include <linux/idr.h>
39 #include <net/9p/9p.h>
40 #include <net/9p/client.h>
47 static const struct file_operations v9fs_cached_file_operations
;
48 static const struct file_operations v9fs_cached_file_operations_dotl
;
51 * v9fs_file_open - open a file (or directory)
52 * @inode: inode to be opened
53 * @file: file being opened
57 int v9fs_file_open(struct inode
*inode
, struct file
*file
)
60 struct v9fs_session_info
*v9ses
;
64 P9_DPRINTK(P9_DEBUG_VFS
, "inode: %p file: %p\n", inode
, file
);
65 v9ses
= v9fs_inode2v9ses(inode
);
66 if (v9fs_proto_dotl(v9ses
))
67 omode
= file
->f_flags
;
69 omode
= v9fs_uflags2omode(file
->f_flags
,
70 v9fs_proto_dotu(v9ses
));
71 fid
= file
->private_data
;
73 fid
= v9fs_fid_clone(file
->f_path
.dentry
);
77 err
= p9_client_open(fid
, omode
);
82 if (file
->f_flags
& O_TRUNC
) {
83 i_size_write(inode
, 0);
86 if ((file
->f_flags
& O_APPEND
) &&
87 (!v9fs_proto_dotu(v9ses
) && !v9fs_proto_dotl(v9ses
)))
88 generic_file_llseek(file
, 0, SEEK_END
);
91 file
->private_data
= fid
;
92 if ((fid
->qid
.version
) && (v9ses
->cache
)) {
93 P9_DPRINTK(P9_DEBUG_VFS
, "cached");
94 /* enable cached file options */
95 if(file
->f_op
== &v9fs_file_operations
)
96 file
->f_op
= &v9fs_cached_file_operations
;
97 else if (file
->f_op
== &v9fs_file_operations_dotl
)
98 file
->f_op
= &v9fs_cached_file_operations_dotl
;
100 #ifdef CONFIG_9P_FSCACHE
101 v9fs_cache_inode_set_cookie(inode
, file
);
109 * v9fs_file_lock - lock a file (or directory)
110 * @filp: file to be locked
112 * @fl: file lock structure
114 * Bugs: this looks like a local only lock, we should extend into 9P
115 * by using open exclusive
118 static int v9fs_file_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
121 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
123 P9_DPRINTK(P9_DEBUG_VFS
, "filp: %p lock: %p\n", filp
, fl
);
125 /* No mandatory locks */
126 if (__mandatory_lock(inode
) && fl
->fl_type
!= F_UNLCK
)
129 if ((IS_SETLK(cmd
) || IS_SETLKW(cmd
)) && fl
->fl_type
!= F_UNLCK
) {
130 filemap_write_and_wait(inode
->i_mapping
);
131 invalidate_mapping_pages(&inode
->i_data
, 0, -1);
137 static int v9fs_file_do_lock(struct file
*filp
, int cmd
, struct file_lock
*fl
)
139 struct p9_flock flock
;
143 unsigned char fl_type
;
145 fid
= filp
->private_data
;
148 if ((fl
->fl_flags
& FL_POSIX
) != FL_POSIX
)
151 res
= posix_lock_file_wait(filp
, fl
);
155 /* convert posix lock to p9 tlock args */
156 memset(&flock
, 0, sizeof(flock
));
157 flock
.type
= fl
->fl_type
;
158 flock
.start
= fl
->fl_start
;
159 if (fl
->fl_end
== OFFSET_MAX
)
162 flock
.length
= fl
->fl_end
- fl
->fl_start
+ 1;
163 flock
.proc_id
= fl
->fl_pid
;
164 flock
.client_id
= utsname()->nodename
;
166 flock
.flags
= P9_LOCK_FLAGS_BLOCK
;
169 * if its a blocked request and we get P9_LOCK_BLOCKED as the status
170 * for lock request, keep on trying
173 res
= p9_client_lock_dotl(fid
, &flock
, &status
);
177 if (status
!= P9_LOCK_BLOCKED
)
179 if (status
== P9_LOCK_BLOCKED
&& !IS_SETLKW(cmd
))
181 schedule_timeout_interruptible(P9_LOCK_TIMEOUT
);
184 /* map 9p status to VFS status */
186 case P9_LOCK_SUCCESS
:
189 case P9_LOCK_BLOCKED
:
201 * incase server returned error for lock request, revert
204 if (res
< 0 && fl
->fl_type
!= F_UNLCK
) {
205 fl_type
= fl
->fl_type
;
206 fl
->fl_type
= F_UNLCK
;
207 res
= posix_lock_file_wait(filp
, fl
);
208 fl
->fl_type
= fl_type
;
214 static int v9fs_file_getlock(struct file
*filp
, struct file_lock
*fl
)
216 struct p9_getlock glock
;
220 fid
= filp
->private_data
;
223 posix_test_lock(filp
, fl
);
225 * if we have a conflicting lock locally, no need to validate
228 if (fl
->fl_type
!= F_UNLCK
)
231 /* convert posix lock to p9 tgetlock args */
232 memset(&glock
, 0, sizeof(glock
));
233 glock
.type
= fl
->fl_type
;
234 glock
.start
= fl
->fl_start
;
235 if (fl
->fl_end
== OFFSET_MAX
)
238 glock
.length
= fl
->fl_end
- fl
->fl_start
+ 1;
239 glock
.proc_id
= fl
->fl_pid
;
240 glock
.client_id
= utsname()->nodename
;
242 res
= p9_client_getlock_dotl(fid
, &glock
);
245 if (glock
.type
!= F_UNLCK
) {
246 fl
->fl_type
= glock
.type
;
247 fl
->fl_start
= glock
.start
;
248 if (glock
.length
== 0)
249 fl
->fl_end
= OFFSET_MAX
;
251 fl
->fl_end
= glock
.start
+ glock
.length
- 1;
252 fl
->fl_pid
= glock
.proc_id
;
254 fl
->fl_type
= F_UNLCK
;
260 * v9fs_file_lock_dotl - lock a file (or directory)
261 * @filp: file to be locked
263 * @fl: file lock structure
267 static int v9fs_file_lock_dotl(struct file
*filp
, int cmd
, struct file_lock
*fl
)
269 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
272 P9_DPRINTK(P9_DEBUG_VFS
, "filp: %p cmd:%d lock: %p name: %s\n", filp
,
273 cmd
, fl
, filp
->f_path
.dentry
->d_name
.name
);
275 /* No mandatory locks */
276 if (__mandatory_lock(inode
) && fl
->fl_type
!= F_UNLCK
)
279 if ((IS_SETLK(cmd
) || IS_SETLKW(cmd
)) && fl
->fl_type
!= F_UNLCK
) {
280 filemap_write_and_wait(inode
->i_mapping
);
281 invalidate_mapping_pages(&inode
->i_data
, 0, -1);
284 if (IS_SETLK(cmd
) || IS_SETLKW(cmd
))
285 ret
= v9fs_file_do_lock(filp
, cmd
, fl
);
286 else if (IS_GETLK(cmd
))
287 ret
= v9fs_file_getlock(filp
, fl
);
295 * v9fs_file_flock_dotl - lock a file
296 * @filp: file to be locked
298 * @fl: file lock structure
302 static int v9fs_file_flock_dotl(struct file
*filp
, int cmd
,
303 struct file_lock
*fl
)
305 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
308 P9_DPRINTK(P9_DEBUG_VFS
, "filp: %p cmd:%d lock: %p name: %s\n", filp
,
309 cmd
, fl
, filp
->f_path
.dentry
->d_name
.name
);
311 /* No mandatory locks */
312 if (__mandatory_lock(inode
) && fl
->fl_type
!= F_UNLCK
)
315 if (!(fl
->fl_flags
& FL_FLOCK
))
318 if ((IS_SETLK(cmd
) || IS_SETLKW(cmd
)) && fl
->fl_type
!= F_UNLCK
) {
319 filemap_write_and_wait(inode
->i_mapping
);
320 invalidate_mapping_pages(&inode
->i_data
, 0, -1);
322 /* Convert flock to posix lock */
323 fl
->fl_owner
= (fl_owner_t
)filp
;
325 fl
->fl_end
= OFFSET_MAX
;
326 fl
->fl_flags
|= FL_POSIX
;
327 fl
->fl_flags
^= FL_FLOCK
;
329 if (IS_SETLK(cmd
) | IS_SETLKW(cmd
))
330 ret
= v9fs_file_do_lock(filp
, cmd
, fl
);
338 * v9fs_file_readn - read from a file
339 * @filp: file pointer to read
340 * @data: data buffer to read data into
341 * @udata: user data buffer to read data into
342 * @count: size of buffer
343 * @offset: offset at which to read data
348 v9fs_file_readn(struct file
*filp
, char *data
, char __user
*udata
, u32 count
,
352 struct p9_fid
*fid
= filp
->private_data
;
354 P9_DPRINTK(P9_DEBUG_VFS
, "fid %d offset %llu count %d\n", fid
->fid
,
355 (long long unsigned) offset
, count
);
359 size
= fid
->iounit
? fid
->iounit
: fid
->clnt
->msize
- P9_IOHDRSZ
;
361 n
= p9_client_read(fid
, data
, udata
, offset
, count
);
373 } while (count
> 0 && n
== size
);
382 * v9fs_file_read - read from a file
383 * @filp: file pointer to read
384 * @udata: user data buffer to read data into
385 * @count: size of buffer
386 * @offset: offset at which to read data
391 v9fs_file_read(struct file
*filp
, char __user
*udata
, size_t count
,
398 P9_DPRINTK(P9_DEBUG_VFS
, "count %zu offset %lld\n", count
, *offset
);
399 fid
= filp
->private_data
;
401 size
= fid
->iounit
? fid
->iounit
: fid
->clnt
->msize
- P9_IOHDRSZ
;
403 ret
= v9fs_file_readn(filp
, NULL
, udata
, count
, *offset
);
405 ret
= p9_client_read(fid
, NULL
, udata
, *offset
, count
);
414 * v9fs_file_write - write to a file
415 * @filp: file pointer to write
416 * @data: data buffer to write data from
417 * @count: size of buffer
418 * @offset: offset at which to write data
423 v9fs_file_write(struct file
*filp
, const char __user
* data
,
424 size_t count
, loff_t
* offset
)
430 struct p9_client
*clnt
;
431 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
432 loff_t origin
= *offset
;
433 unsigned long pg_start
, pg_end
;
435 P9_DPRINTK(P9_DEBUG_VFS
, "data %p count %d offset %x\n", data
,
436 (int)count
, (int)*offset
);
438 fid
= filp
->private_data
;
441 retval
= generic_write_checks(filp
, &origin
, &count
, 0);
446 if ((ssize_t
) count
< 0)
453 n
= p9_client_write(fid
, NULL
, data
+total
, origin
+total
, count
);
461 pg_start
= origin
>> PAGE_CACHE_SHIFT
;
462 pg_end
= (origin
+ total
- 1) >> PAGE_CACHE_SHIFT
;
463 if (inode
->i_mapping
&& inode
->i_mapping
->nrpages
)
464 invalidate_inode_pages2_range(inode
->i_mapping
,
467 i_size_write(inode
, i_size_read(inode
) + total
);
468 inode
->i_blocks
= (i_size_read(inode
) + 512 - 1) >> 9;
479 static int v9fs_file_fsync(struct file
*filp
, int datasync
)
482 struct p9_wstat wstat
;
485 P9_DPRINTK(P9_DEBUG_VFS
, "filp %p datasync %x\n", filp
, datasync
);
487 fid
= filp
->private_data
;
488 v9fs_blank_wstat(&wstat
);
490 retval
= p9_client_wstat(fid
, &wstat
);
494 int v9fs_file_fsync_dotl(struct file
*filp
, int datasync
)
499 P9_DPRINTK(P9_DEBUG_VFS
, "v9fs_file_fsync_dotl: filp %p datasync %x\n",
502 fid
= filp
->private_data
;
504 retval
= p9_client_fsync(fid
, datasync
);
508 static const struct file_operations v9fs_cached_file_operations
= {
509 .llseek
= generic_file_llseek
,
510 .read
= do_sync_read
,
511 .aio_read
= generic_file_aio_read
,
512 .write
= v9fs_file_write
,
513 .open
= v9fs_file_open
,
514 .release
= v9fs_dir_release
,
515 .lock
= v9fs_file_lock
,
516 .mmap
= generic_file_readonly_mmap
,
517 .fsync
= v9fs_file_fsync
,
520 static const struct file_operations v9fs_cached_file_operations_dotl
= {
521 .llseek
= generic_file_llseek
,
522 .read
= do_sync_read
,
523 .aio_read
= generic_file_aio_read
,
524 .write
= v9fs_file_write
,
525 .open
= v9fs_file_open
,
526 .release
= v9fs_dir_release
,
527 .lock
= v9fs_file_lock_dotl
,
528 .flock
= v9fs_file_flock_dotl
,
529 .mmap
= generic_file_readonly_mmap
,
530 .fsync
= v9fs_file_fsync_dotl
,
533 const struct file_operations v9fs_file_operations
= {
534 .llseek
= generic_file_llseek
,
535 .read
= v9fs_file_read
,
536 .write
= v9fs_file_write
,
537 .open
= v9fs_file_open
,
538 .release
= v9fs_dir_release
,
539 .lock
= v9fs_file_lock
,
540 .mmap
= generic_file_readonly_mmap
,
541 .fsync
= v9fs_file_fsync
,
544 const struct file_operations v9fs_file_operations_dotl
= {
545 .llseek
= generic_file_llseek
,
546 .read
= v9fs_file_read
,
547 .write
= v9fs_file_write
,
548 .open
= v9fs_file_open
,
549 .release
= v9fs_dir_release
,
550 .lock
= v9fs_file_lock_dotl
,
551 .flock
= v9fs_file_flock_dotl
,
552 .mmap
= generic_file_readonly_mmap
,
553 .fsync
= v9fs_file_fsync_dotl
,