vfs_file.c 13 KB

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