vfs_file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file contians vfs file ops for 9P2000.
  4. *
  5. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  6. * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/filelock.h>
  12. #include <linux/sched.h>
  13. #include <linux/file.h>
  14. #include <linux/stat.h>
  15. #include <linux/string.h>
  16. #include <linux/list.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/utsname.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/uio.h>
  21. #include <linux/slab.h>
  22. #include <net/9p/9p.h>
  23. #include <net/9p/client.h>
  24. #include "v9fs.h"
  25. #include "v9fs_vfs.h"
  26. #include "fid.h"
  27. #include "cache.h"
  28. static const struct vm_operations_struct v9fs_mmap_file_vm_ops;
  29. /**
  30. * v9fs_file_open - open a file (or directory)
  31. * @inode: inode to be opened
  32. * @file: file being opened
  33. *
  34. */
  35. int v9fs_file_open(struct inode *inode, struct file *file)
  36. {
  37. int err;
  38. struct v9fs_session_info *v9ses;
  39. struct p9_fid *fid;
  40. int omode;
  41. p9_debug(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
  42. v9ses = v9fs_inode2v9ses(inode);
  43. if (v9fs_proto_dotl(v9ses))
  44. omode = v9fs_open_to_dotl_flags(file->f_flags);
  45. else
  46. omode = v9fs_uflags2omode(file->f_flags,
  47. v9fs_proto_dotu(v9ses));
  48. fid = file->private_data;
  49. if (!fid) {
  50. fid = v9fs_fid_clone(file_dentry(file));
  51. if (IS_ERR(fid))
  52. return PTR_ERR(fid);
  53. if ((v9ses->cache & CACHE_WRITEBACK) && (omode & P9_OWRITE)) {
  54. int writeback_omode = (omode & ~P9_OWRITE) | P9_ORDWR;
  55. p9_debug(P9_DEBUG_CACHE, "write-only file with writeback enabled, try opening O_RDWR\n");
  56. err = p9_client_open(fid, writeback_omode);
  57. if (err < 0) {
  58. p9_debug(P9_DEBUG_CACHE, "could not open O_RDWR, disabling caches\n");
  59. err = p9_client_open(fid, omode);
  60. fid->mode |= P9L_DIRECT;
  61. }
  62. } else {
  63. err = p9_client_open(fid, omode);
  64. }
  65. if (err < 0) {
  66. p9_fid_put(fid);
  67. return err;
  68. }
  69. if ((file->f_flags & O_APPEND) &&
  70. (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
  71. generic_file_llseek(file, 0, SEEK_END);
  72. file->private_data = fid;
  73. }
  74. #ifdef CONFIG_9P_FSCACHE
  75. if (v9ses->cache & CACHE_FSCACHE)
  76. fscache_use_cookie(v9fs_inode_cookie(V9FS_I(inode)),
  77. file->f_mode & FMODE_WRITE);
  78. #endif
  79. v9fs_fid_add_modes(fid, v9ses->flags, v9ses->cache, file->f_flags);
  80. v9fs_open_fid_add(inode, &fid);
  81. return 0;
  82. }
  83. /**
  84. * v9fs_file_lock - lock a file (or directory)
  85. * @filp: file to be locked
  86. * @cmd: lock command
  87. * @fl: file lock structure
  88. *
  89. * Bugs: this looks like a local only lock, we should extend into 9P
  90. * by using open exclusive
  91. */
  92. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  93. {
  94. struct inode *inode = file_inode(filp);
  95. p9_debug(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  96. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->c.flc_type != F_UNLCK) {
  97. filemap_write_and_wait(inode->i_mapping);
  98. invalidate_mapping_pages(&inode->i_data, 0, -1);
  99. }
  100. return 0;
  101. }
  102. static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
  103. {
  104. struct p9_flock flock;
  105. struct p9_fid *fid;
  106. uint8_t status = P9_LOCK_ERROR;
  107. int res = 0;
  108. struct v9fs_session_info *v9ses;
  109. fid = filp->private_data;
  110. BUG_ON(fid == NULL);
  111. BUG_ON((fl->c.flc_flags & FL_POSIX) != FL_POSIX);
  112. res = locks_lock_file_wait(filp, fl);
  113. if (res < 0)
  114. goto out;
  115. /* convert posix lock to p9 tlock args */
  116. memset(&flock, 0, sizeof(flock));
  117. /* map the lock type */
  118. switch (fl->c.flc_type) {
  119. case F_RDLCK:
  120. flock.type = P9_LOCK_TYPE_RDLCK;
  121. break;
  122. case F_WRLCK:
  123. flock.type = P9_LOCK_TYPE_WRLCK;
  124. break;
  125. case F_UNLCK:
  126. flock.type = P9_LOCK_TYPE_UNLCK;
  127. break;
  128. }
  129. flock.start = fl->fl_start;
  130. if (fl->fl_end == OFFSET_MAX)
  131. flock.length = 0;
  132. else
  133. flock.length = fl->fl_end - fl->fl_start + 1;
  134. flock.proc_id = fl->c.flc_pid;
  135. flock.client_id = fid->clnt->name;
  136. if (IS_SETLKW(cmd))
  137. flock.flags = P9_LOCK_FLAGS_BLOCK;
  138. v9ses = v9fs_inode2v9ses(file_inode(filp));
  139. /*
  140. * if its a blocked request and we get P9_LOCK_BLOCKED as the status
  141. * for lock request, keep on trying
  142. */
  143. for (;;) {
  144. res = p9_client_lock_dotl(fid, &flock, &status);
  145. if (res < 0)
  146. goto out_unlock;
  147. if (status != P9_LOCK_BLOCKED)
  148. break;
  149. if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
  150. break;
  151. if (schedule_timeout_interruptible(v9ses->session_lock_timeout)
  152. != 0)
  153. break;
  154. /*
  155. * p9_client_lock_dotl overwrites flock.client_id with the
  156. * server message, free and reuse the client name
  157. */
  158. if (flock.client_id != fid->clnt->name) {
  159. kfree(flock.client_id);
  160. flock.client_id = fid->clnt->name;
  161. }
  162. }
  163. /* map 9p status to VFS status */
  164. switch (status) {
  165. case P9_LOCK_SUCCESS:
  166. res = 0;
  167. break;
  168. case P9_LOCK_BLOCKED:
  169. res = -EAGAIN;
  170. break;
  171. default:
  172. WARN_ONCE(1, "unknown lock status code: %d\n", status);
  173. fallthrough;
  174. case P9_LOCK_ERROR:
  175. case P9_LOCK_GRACE:
  176. res = -ENOLCK;
  177. break;
  178. }
  179. out_unlock:
  180. /*
  181. * incase server returned error for lock request, revert
  182. * it locally
  183. */
  184. if (res < 0 && fl->c.flc_type != F_UNLCK) {
  185. unsigned char type = fl->c.flc_type;
  186. fl->c.flc_type = F_UNLCK;
  187. /* Even if this fails we want to return the remote error */
  188. locks_lock_file_wait(filp, fl);
  189. fl->c.flc_type = type;
  190. }
  191. if (flock.client_id != fid->clnt->name)
  192. kfree(flock.client_id);
  193. out:
  194. return res;
  195. }
  196. static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
  197. {
  198. struct p9_getlock glock;
  199. struct p9_fid *fid;
  200. int res = 0;
  201. fid = filp->private_data;
  202. BUG_ON(fid == NULL);
  203. posix_test_lock(filp, fl);
  204. /*
  205. * if we have a conflicting lock locally, no need to validate
  206. * with server
  207. */
  208. if (fl->c.flc_type != F_UNLCK)
  209. return res;
  210. /* convert posix lock to p9 tgetlock args */
  211. memset(&glock, 0, sizeof(glock));
  212. glock.type = P9_LOCK_TYPE_UNLCK;
  213. glock.start = fl->fl_start;
  214. if (fl->fl_end == OFFSET_MAX)
  215. glock.length = 0;
  216. else
  217. glock.length = fl->fl_end - fl->fl_start + 1;
  218. glock.proc_id = fl->c.flc_pid;
  219. glock.client_id = fid->clnt->name;
  220. res = p9_client_getlock_dotl(fid, &glock);
  221. if (res < 0)
  222. goto out;
  223. /* map 9p lock type to os lock type */
  224. switch (glock.type) {
  225. case P9_LOCK_TYPE_RDLCK:
  226. fl->c.flc_type = F_RDLCK;
  227. break;
  228. case P9_LOCK_TYPE_WRLCK:
  229. fl->c.flc_type = F_WRLCK;
  230. break;
  231. case P9_LOCK_TYPE_UNLCK:
  232. fl->c.flc_type = F_UNLCK;
  233. break;
  234. }
  235. if (glock.type != P9_LOCK_TYPE_UNLCK) {
  236. fl->fl_start = glock.start;
  237. if (glock.length == 0)
  238. fl->fl_end = OFFSET_MAX;
  239. else
  240. fl->fl_end = glock.start + glock.length - 1;
  241. fl->c.flc_pid = -glock.proc_id;
  242. }
  243. out:
  244. if (glock.client_id != fid->clnt->name)
  245. kfree(glock.client_id);
  246. return res;
  247. }
  248. /**
  249. * v9fs_file_lock_dotl - lock a file (or directory)
  250. * @filp: file to be locked
  251. * @cmd: lock command
  252. * @fl: file lock structure
  253. *
  254. */
  255. static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
  256. {
  257. struct inode *inode = file_inode(filp);
  258. int ret = -ENOLCK;
  259. p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
  260. filp, cmd, fl, filp);
  261. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->c.flc_type != F_UNLCK) {
  262. filemap_write_and_wait(inode->i_mapping);
  263. invalidate_mapping_pages(&inode->i_data, 0, -1);
  264. }
  265. if (IS_SETLK(cmd) || IS_SETLKW(cmd))
  266. ret = v9fs_file_do_lock(filp, cmd, fl);
  267. else if (IS_GETLK(cmd))
  268. ret = v9fs_file_getlock(filp, fl);
  269. else
  270. ret = -EINVAL;
  271. return ret;
  272. }
  273. /**
  274. * v9fs_file_flock_dotl - lock a file
  275. * @filp: file to be locked
  276. * @cmd: lock command
  277. * @fl: file lock structure
  278. *
  279. */
  280. static int v9fs_file_flock_dotl(struct file *filp, int cmd,
  281. struct file_lock *fl)
  282. {
  283. struct inode *inode = file_inode(filp);
  284. int ret = -ENOLCK;
  285. p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
  286. filp, cmd, fl, filp);
  287. if (!(fl->c.flc_flags & FL_FLOCK))
  288. goto out_err;
  289. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->c.flc_type != F_UNLCK) {
  290. filemap_write_and_wait(inode->i_mapping);
  291. invalidate_mapping_pages(&inode->i_data, 0, -1);
  292. }
  293. /* Convert flock to posix lock */
  294. fl->c.flc_flags |= FL_POSIX;
  295. fl->c.flc_flags ^= FL_FLOCK;
  296. if (IS_SETLK(cmd) | IS_SETLKW(cmd))
  297. ret = v9fs_file_do_lock(filp, cmd, fl);
  298. else
  299. ret = -EINVAL;
  300. out_err:
  301. return ret;
  302. }
  303. /**
  304. * v9fs_file_read_iter - read from a file
  305. * @iocb: The operation parameters
  306. * @to: The buffer to read into
  307. *
  308. */
  309. static ssize_t
  310. v9fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  311. {
  312. struct p9_fid *fid = iocb->ki_filp->private_data;
  313. p9_debug(P9_DEBUG_VFS, "fid %d count %zu offset %lld\n",
  314. fid->fid, iov_iter_count(to), iocb->ki_pos);
  315. if (fid->mode & P9L_DIRECT)
  316. return netfs_unbuffered_read_iter(iocb, to);
  317. p9_debug(P9_DEBUG_VFS, "(cached)\n");
  318. return netfs_file_read_iter(iocb, to);
  319. }
  320. /*
  321. * v9fs_file_splice_read - splice-read from a file
  322. * @in: The 9p file to read from
  323. * @ppos: Where to find/update the file position
  324. * @pipe: The pipe to splice into
  325. * @len: The maximum amount of data to splice
  326. * @flags: SPLICE_F_* flags
  327. */
  328. static ssize_t v9fs_file_splice_read(struct file *in, loff_t *ppos,
  329. struct pipe_inode_info *pipe,
  330. size_t len, unsigned int flags)
  331. {
  332. struct p9_fid *fid = in->private_data;
  333. p9_debug(P9_DEBUG_VFS, "fid %d count %zu offset %lld\n",
  334. fid->fid, len, *ppos);
  335. if (fid->mode & P9L_DIRECT)
  336. return copy_splice_read(in, ppos, pipe, len, flags);
  337. return filemap_splice_read(in, ppos, pipe, len, flags);
  338. }
  339. /**
  340. * v9fs_file_write_iter - write to a file
  341. * @iocb: The operation parameters
  342. * @from: The data to write
  343. *
  344. */
  345. static ssize_t
  346. v9fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  347. {
  348. struct file *file = iocb->ki_filp;
  349. struct p9_fid *fid = file->private_data;
  350. p9_debug(P9_DEBUG_VFS, "fid %d\n", fid->fid);
  351. if (fid->mode & (P9L_DIRECT | P9L_NOWRITECACHE))
  352. return netfs_unbuffered_write_iter(iocb, from);
  353. p9_debug(P9_DEBUG_CACHE, "(cached)\n");
  354. return netfs_file_write_iter(iocb, from);
  355. }
  356. static int v9fs_file_fsync(struct file *filp, loff_t start, loff_t end,
  357. int datasync)
  358. {
  359. struct p9_fid *fid;
  360. struct inode *inode = filp->f_mapping->host;
  361. struct p9_wstat wstat;
  362. int retval;
  363. retval = file_write_and_wait_range(filp, start, end);
  364. if (retval)
  365. return retval;
  366. inode_lock(inode);
  367. p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
  368. fid = filp->private_data;
  369. v9fs_blank_wstat(&wstat);
  370. retval = p9_client_wstat(fid, &wstat);
  371. inode_unlock(inode);
  372. return retval;
  373. }
  374. int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
  375. int datasync)
  376. {
  377. struct p9_fid *fid;
  378. struct inode *inode = filp->f_mapping->host;
  379. int retval;
  380. retval = file_write_and_wait_range(filp, start, end);
  381. if (retval)
  382. return retval;
  383. inode_lock(inode);
  384. p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
  385. fid = filp->private_data;
  386. retval = p9_client_fsync(fid, datasync);
  387. inode_unlock(inode);
  388. return retval;
  389. }
  390. static int
  391. v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)
  392. {
  393. int retval;
  394. struct inode *inode = file_inode(filp);
  395. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  396. p9_debug(P9_DEBUG_MMAP, "filp :%p\n", filp);
  397. if (!(v9ses->cache & CACHE_WRITEBACK)) {
  398. p9_debug(P9_DEBUG_CACHE, "(read-only mmap mode)");
  399. return generic_file_readonly_mmap(filp, vma);
  400. }
  401. retval = generic_file_mmap(filp, vma);
  402. if (!retval)
  403. vma->vm_ops = &v9fs_mmap_file_vm_ops;
  404. return retval;
  405. }
  406. static vm_fault_t
  407. v9fs_vm_page_mkwrite(struct vm_fault *vmf)
  408. {
  409. return netfs_page_mkwrite(vmf, NULL);
  410. }
  411. static void v9fs_mmap_vm_close(struct vm_area_struct *vma)
  412. {
  413. struct inode *inode;
  414. struct writeback_control wbc = {
  415. .nr_to_write = LONG_MAX,
  416. .sync_mode = WB_SYNC_ALL,
  417. .range_start = (loff_t)vma->vm_pgoff * PAGE_SIZE,
  418. /* absolute end, byte at end included */
  419. .range_end = (loff_t)vma->vm_pgoff * PAGE_SIZE +
  420. (vma->vm_end - vma->vm_start - 1),
  421. };
  422. if (!(vma->vm_flags & VM_SHARED))
  423. return;
  424. p9_debug(P9_DEBUG_VFS, "9p VMA close, %p, flushing", vma);
  425. inode = file_inode(vma->vm_file);
  426. filemap_fdatawrite_wbc(inode->i_mapping, &wbc);
  427. }
  428. static const struct vm_operations_struct v9fs_mmap_file_vm_ops = {
  429. .close = v9fs_mmap_vm_close,
  430. .fault = filemap_fault,
  431. .map_pages = filemap_map_pages,
  432. .page_mkwrite = v9fs_vm_page_mkwrite,
  433. };
  434. const struct file_operations v9fs_file_operations = {
  435. .llseek = generic_file_llseek,
  436. .read_iter = v9fs_file_read_iter,
  437. .write_iter = v9fs_file_write_iter,
  438. .open = v9fs_file_open,
  439. .release = v9fs_dir_release,
  440. .lock = v9fs_file_lock,
  441. .mmap = generic_file_readonly_mmap,
  442. .splice_read = v9fs_file_splice_read,
  443. .splice_write = iter_file_splice_write,
  444. .fsync = v9fs_file_fsync,
  445. .setlease = simple_nosetlease,
  446. };
  447. const struct file_operations v9fs_file_operations_dotl = {
  448. .llseek = generic_file_llseek,
  449. .read_iter = v9fs_file_read_iter,
  450. .write_iter = v9fs_file_write_iter,
  451. .open = v9fs_file_open,
  452. .release = v9fs_dir_release,
  453. .lock = v9fs_file_lock_dotl,
  454. .flock = v9fs_file_flock_dotl,
  455. .mmap = v9fs_file_mmap,
  456. .splice_read = v9fs_file_splice_read,
  457. .splice_write = iter_file_splice_write,
  458. .fsync = v9fs_file_fsync_dotl,
  459. .setlease = simple_nosetlease,
  460. };