vfs_file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * linux/fs/9p/vfs_file.c
  3. *
  4. * This file contians vfs file ops for 9P2000.
  5. *
  6. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  7. * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
  8. *
  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.
  12. *
  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.
  17. *
  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
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/errno.h>
  27. #include <linux/fs.h>
  28. #include <linux/sched.h>
  29. #include <linux/file.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/inet.h>
  33. #include <linux/list.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/utsname.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/idr.h>
  38. #include <net/9p/9p.h>
  39. #include <net/9p/client.h>
  40. #include "v9fs.h"
  41. #include "v9fs_vfs.h"
  42. #include "fid.h"
  43. #include "cache.h"
  44. static const struct vm_operations_struct v9fs_file_vm_ops;
  45. /**
  46. * v9fs_file_open - open a file (or directory)
  47. * @inode: inode to be opened
  48. * @file: file being opened
  49. *
  50. */
  51. int v9fs_file_open(struct inode *inode, struct file *file)
  52. {
  53. int err;
  54. struct v9fs_inode *v9inode;
  55. struct v9fs_session_info *v9ses;
  56. struct p9_fid *fid;
  57. int omode;
  58. P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
  59. v9inode = V9FS_I(inode);
  60. v9ses = v9fs_inode2v9ses(inode);
  61. if (v9fs_proto_dotl(v9ses))
  62. omode = file->f_flags;
  63. else
  64. omode = v9fs_uflags2omode(file->f_flags,
  65. v9fs_proto_dotu(v9ses));
  66. fid = file->private_data;
  67. if (!fid) {
  68. fid = v9fs_fid_clone(file->f_path.dentry);
  69. if (IS_ERR(fid))
  70. return PTR_ERR(fid);
  71. err = p9_client_open(fid, omode);
  72. if (err < 0) {
  73. p9_client_clunk(fid);
  74. return err;
  75. }
  76. if (file->f_flags & O_TRUNC) {
  77. i_size_write(inode, 0);
  78. inode->i_blocks = 0;
  79. }
  80. if ((file->f_flags & O_APPEND) &&
  81. (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
  82. generic_file_llseek(file, 0, SEEK_END);
  83. }
  84. file->private_data = fid;
  85. mutex_lock(&v9inode->v_mutex);
  86. if (v9ses->cache && !v9inode->writeback_fid &&
  87. ((file->f_flags & O_ACCMODE) != O_RDONLY)) {
  88. /*
  89. * clone a fid and add it to writeback_fid
  90. * we do it during open time instead of
  91. * page dirty time via write_begin/page_mkwrite
  92. * because we want write after unlink usecase
  93. * to work.
  94. */
  95. fid = v9fs_writeback_fid(file->f_path.dentry);
  96. if (IS_ERR(fid)) {
  97. err = PTR_ERR(fid);
  98. mutex_unlock(&v9inode->v_mutex);
  99. goto out_error;
  100. }
  101. v9inode->writeback_fid = (void *) fid;
  102. }
  103. mutex_unlock(&v9inode->v_mutex);
  104. #ifdef CONFIG_9P_FSCACHE
  105. if (v9ses->cache)
  106. v9fs_cache_inode_set_cookie(inode, file);
  107. #endif
  108. return 0;
  109. out_error:
  110. p9_client_clunk(file->private_data);
  111. file->private_data = NULL;
  112. return err;
  113. }
  114. /**
  115. * v9fs_file_lock - lock a file (or directory)
  116. * @filp: file to be locked
  117. * @cmd: lock command
  118. * @fl: file lock structure
  119. *
  120. * Bugs: this looks like a local only lock, we should extend into 9P
  121. * by using open exclusive
  122. */
  123. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  124. {
  125. int res = 0;
  126. struct inode *inode = filp->f_path.dentry->d_inode;
  127. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  128. /* No mandatory locks */
  129. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  130. return -ENOLCK;
  131. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  132. filemap_write_and_wait(inode->i_mapping);
  133. invalidate_mapping_pages(&inode->i_data, 0, -1);
  134. }
  135. return res;
  136. }
  137. static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
  138. {
  139. struct p9_flock flock;
  140. struct p9_fid *fid;
  141. uint8_t status;
  142. int res = 0;
  143. unsigned char fl_type;
  144. fid = filp->private_data;
  145. BUG_ON(fid == NULL);
  146. if ((fl->fl_flags & FL_POSIX) != FL_POSIX)
  147. BUG();
  148. res = posix_lock_file_wait(filp, fl);
  149. if (res < 0)
  150. goto out;
  151. /* convert posix lock to p9 tlock args */
  152. memset(&flock, 0, sizeof(flock));
  153. flock.type = fl->fl_type;
  154. flock.start = fl->fl_start;
  155. if (fl->fl_end == OFFSET_MAX)
  156. flock.length = 0;
  157. else
  158. flock.length = fl->fl_end - fl->fl_start + 1;
  159. flock.proc_id = fl->fl_pid;
  160. flock.client_id = utsname()->nodename;
  161. if (IS_SETLKW(cmd))
  162. flock.flags = P9_LOCK_FLAGS_BLOCK;
  163. /*
  164. * if its a blocked request and we get P9_LOCK_BLOCKED as the status
  165. * for lock request, keep on trying
  166. */
  167. for (;;) {
  168. res = p9_client_lock_dotl(fid, &flock, &status);
  169. if (res < 0)
  170. break;
  171. if (status != P9_LOCK_BLOCKED)
  172. break;
  173. if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
  174. break;
  175. schedule_timeout_interruptible(P9_LOCK_TIMEOUT);
  176. }
  177. /* map 9p status to VFS status */
  178. switch (status) {
  179. case P9_LOCK_SUCCESS:
  180. res = 0;
  181. break;
  182. case P9_LOCK_BLOCKED:
  183. res = -EAGAIN;
  184. break;
  185. case P9_LOCK_ERROR:
  186. case P9_LOCK_GRACE:
  187. res = -ENOLCK;
  188. break;
  189. default:
  190. BUG();
  191. }
  192. /*
  193. * incase server returned error for lock request, revert
  194. * it locally
  195. */
  196. if (res < 0 && fl->fl_type != F_UNLCK) {
  197. fl_type = fl->fl_type;
  198. fl->fl_type = F_UNLCK;
  199. res = posix_lock_file_wait(filp, fl);
  200. fl->fl_type = fl_type;
  201. }
  202. out:
  203. return res;
  204. }
  205. static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
  206. {
  207. struct p9_getlock glock;
  208. struct p9_fid *fid;
  209. int res = 0;
  210. fid = filp->private_data;
  211. BUG_ON(fid == NULL);
  212. posix_test_lock(filp, fl);
  213. /*
  214. * if we have a conflicting lock locally, no need to validate
  215. * with server
  216. */
  217. if (fl->fl_type != F_UNLCK)
  218. return res;
  219. /* convert posix lock to p9 tgetlock args */
  220. memset(&glock, 0, sizeof(glock));
  221. glock.type = fl->fl_type;
  222. glock.start = fl->fl_start;
  223. if (fl->fl_end == OFFSET_MAX)
  224. glock.length = 0;
  225. else
  226. glock.length = fl->fl_end - fl->fl_start + 1;
  227. glock.proc_id = fl->fl_pid;
  228. glock.client_id = utsname()->nodename;
  229. res = p9_client_getlock_dotl(fid, &glock);
  230. if (res < 0)
  231. return res;
  232. if (glock.type != F_UNLCK) {
  233. fl->fl_type = glock.type;
  234. fl->fl_start = glock.start;
  235. if (glock.length == 0)
  236. fl->fl_end = OFFSET_MAX;
  237. else
  238. fl->fl_end = glock.start + glock.length - 1;
  239. fl->fl_pid = glock.proc_id;
  240. } else
  241. fl->fl_type = F_UNLCK;
  242. return res;
  243. }
  244. /**
  245. * v9fs_file_lock_dotl - lock a file (or directory)
  246. * @filp: file to be locked
  247. * @cmd: lock command
  248. * @fl: file lock structure
  249. *
  250. */
  251. static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
  252. {
  253. struct inode *inode = filp->f_path.dentry->d_inode;
  254. int ret = -ENOLCK;
  255. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
  256. cmd, fl, filp->f_path.dentry->d_name.name);
  257. /* No mandatory locks */
  258. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  259. goto out_err;
  260. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  261. filemap_write_and_wait(inode->i_mapping);
  262. invalidate_mapping_pages(&inode->i_data, 0, -1);
  263. }
  264. if (IS_SETLK(cmd) || IS_SETLKW(cmd))
  265. ret = v9fs_file_do_lock(filp, cmd, fl);
  266. else if (IS_GETLK(cmd))
  267. ret = v9fs_file_getlock(filp, fl);
  268. else
  269. ret = -EINVAL;
  270. out_err:
  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 = filp->f_path.dentry->d_inode;
  284. int ret = -ENOLCK;
  285. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
  286. cmd, fl, filp->f_path.dentry->d_name.name);
  287. /* No mandatory locks */
  288. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  289. goto out_err;
  290. if (!(fl->fl_flags & FL_FLOCK))
  291. goto out_err;
  292. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  293. filemap_write_and_wait(inode->i_mapping);
  294. invalidate_mapping_pages(&inode->i_data, 0, -1);
  295. }
  296. /* Convert flock to posix lock */
  297. fl->fl_owner = (fl_owner_t)filp;
  298. fl->fl_start = 0;
  299. fl->fl_end = OFFSET_MAX;
  300. fl->fl_flags |= FL_POSIX;
  301. fl->fl_flags ^= FL_FLOCK;
  302. if (IS_SETLK(cmd) | IS_SETLKW(cmd))
  303. ret = v9fs_file_do_lock(filp, cmd, fl);
  304. else
  305. ret = -EINVAL;
  306. out_err:
  307. return ret;
  308. }
  309. /**
  310. * v9fs_fid_readn - read from a fid
  311. * @fid: fid to read
  312. * @data: data buffer to read data into
  313. * @udata: user data buffer to read data into
  314. * @count: size of buffer
  315. * @offset: offset at which to read data
  316. *
  317. */
  318. ssize_t
  319. v9fs_fid_readn(struct p9_fid *fid, char *data, char __user *udata, u32 count,
  320. u64 offset)
  321. {
  322. int n, total, size;
  323. P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
  324. (long long unsigned) offset, count);
  325. n = 0;
  326. total = 0;
  327. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  328. do {
  329. n = p9_client_read(fid, data, udata, offset, count);
  330. if (n <= 0)
  331. break;
  332. if (data)
  333. data += n;
  334. if (udata)
  335. udata += n;
  336. offset += n;
  337. count -= n;
  338. total += n;
  339. } while (count > 0 && n == size);
  340. if (n < 0)
  341. total = n;
  342. return total;
  343. }
  344. /**
  345. * v9fs_file_readn - read from a file
  346. * @filp: file pointer to read
  347. * @data: data buffer to read data into
  348. * @udata: user data buffer to read data into
  349. * @count: size of buffer
  350. * @offset: offset at which to read data
  351. *
  352. */
  353. ssize_t
  354. v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
  355. u64 offset)
  356. {
  357. return v9fs_fid_readn(filp->private_data, data, udata, count, offset);
  358. }
  359. /**
  360. * v9fs_file_read - read from a file
  361. * @filp: file pointer to read
  362. * @udata: user data buffer to read data into
  363. * @count: size of buffer
  364. * @offset: offset at which to read data
  365. *
  366. */
  367. static ssize_t
  368. v9fs_file_read(struct file *filp, char __user *udata, size_t count,
  369. loff_t * offset)
  370. {
  371. int ret;
  372. struct p9_fid *fid;
  373. size_t size;
  374. P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
  375. fid = filp->private_data;
  376. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  377. if (count > size)
  378. ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
  379. else
  380. ret = p9_client_read(fid, NULL, udata, *offset, count);
  381. if (ret > 0)
  382. *offset += ret;
  383. return ret;
  384. }
  385. ssize_t
  386. v9fs_file_write_internal(struct inode *inode, struct p9_fid *fid,
  387. const char __user *data, size_t count,
  388. loff_t *offset, int invalidate)
  389. {
  390. int n;
  391. loff_t i_size;
  392. size_t total = 0;
  393. struct p9_client *clnt;
  394. loff_t origin = *offset;
  395. unsigned long pg_start, pg_end;
  396. P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
  397. (int)count, (int)*offset);
  398. clnt = fid->clnt;
  399. do {
  400. n = p9_client_write(fid, NULL, data+total, origin+total, count);
  401. if (n <= 0)
  402. break;
  403. count -= n;
  404. total += n;
  405. } while (count > 0);
  406. if (invalidate && (total > 0)) {
  407. pg_start = origin >> PAGE_CACHE_SHIFT;
  408. pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
  409. if (inode->i_mapping && inode->i_mapping->nrpages)
  410. invalidate_inode_pages2_range(inode->i_mapping,
  411. pg_start, pg_end);
  412. *offset += total;
  413. i_size = i_size_read(inode);
  414. if (*offset > i_size) {
  415. inode_add_bytes(inode, *offset - i_size);
  416. i_size_write(inode, *offset);
  417. }
  418. }
  419. if (n < 0)
  420. return n;
  421. return total;
  422. }
  423. /**
  424. * v9fs_file_write - write to a file
  425. * @filp: file pointer to write
  426. * @data: data buffer to write data from
  427. * @count: size of buffer
  428. * @offset: offset at which to write data
  429. *
  430. */
  431. static ssize_t
  432. v9fs_file_write(struct file *filp, const char __user * data,
  433. size_t count, loff_t *offset)
  434. {
  435. ssize_t retval = 0;
  436. loff_t origin = *offset;
  437. retval = generic_write_checks(filp, &origin, &count, 0);
  438. if (retval)
  439. goto out;
  440. retval = -EINVAL;
  441. if ((ssize_t) count < 0)
  442. goto out;
  443. retval = 0;
  444. if (!count)
  445. goto out;
  446. retval = v9fs_file_write_internal(filp->f_path.dentry->d_inode,
  447. filp->private_data,
  448. data, count, &origin, 1);
  449. /* update offset on successful write */
  450. if (retval > 0)
  451. *offset = origin;
  452. out:
  453. return retval;
  454. }
  455. static int v9fs_file_fsync(struct file *filp, loff_t start, loff_t end,
  456. int datasync)
  457. {
  458. struct p9_fid *fid;
  459. struct inode *inode = filp->f_mapping->host;
  460. struct p9_wstat wstat;
  461. int retval;
  462. retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
  463. if (retval)
  464. return retval;
  465. mutex_lock(&inode->i_mutex);
  466. P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
  467. fid = filp->private_data;
  468. v9fs_blank_wstat(&wstat);
  469. retval = p9_client_wstat(fid, &wstat);
  470. mutex_unlock(&inode->i_mutex);
  471. return retval;
  472. }
  473. int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
  474. int datasync)
  475. {
  476. struct p9_fid *fid;
  477. struct inode *inode = filp->f_mapping->host;
  478. int retval;
  479. retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
  480. if (retval)
  481. return retval;
  482. mutex_lock(&inode->i_mutex);
  483. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_file_fsync_dotl: filp %p datasync %x\n",
  484. filp, datasync);
  485. fid = filp->private_data;
  486. retval = p9_client_fsync(fid, datasync);
  487. mutex_unlock(&inode->i_mutex);
  488. return retval;
  489. }
  490. static int
  491. v9fs_file_mmap(struct file *file, struct vm_area_struct *vma)
  492. {
  493. int retval;
  494. retval = generic_file_mmap(file, vma);
  495. if (!retval)
  496. vma->vm_ops = &v9fs_file_vm_ops;
  497. return retval;
  498. }
  499. static int
  500. v9fs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  501. {
  502. struct v9fs_inode *v9inode;
  503. struct page *page = vmf->page;
  504. struct file *filp = vma->vm_file;
  505. struct inode *inode = filp->f_path.dentry->d_inode;
  506. P9_DPRINTK(P9_DEBUG_VFS, "page %p fid %lx\n",
  507. page, (unsigned long)filp->private_data);
  508. v9inode = V9FS_I(inode);
  509. /* make sure the cache has finished storing the page */
  510. v9fs_fscache_wait_on_page_write(inode, page);
  511. BUG_ON(!v9inode->writeback_fid);
  512. lock_page(page);
  513. if (page->mapping != inode->i_mapping)
  514. goto out_unlock;
  515. return VM_FAULT_LOCKED;
  516. out_unlock:
  517. unlock_page(page);
  518. return VM_FAULT_NOPAGE;
  519. }
  520. static ssize_t
  521. v9fs_direct_read(struct file *filp, char __user *udata, size_t count,
  522. loff_t *offsetp)
  523. {
  524. loff_t size, offset;
  525. struct inode *inode;
  526. struct address_space *mapping;
  527. offset = *offsetp;
  528. mapping = filp->f_mapping;
  529. inode = mapping->host;
  530. if (!count)
  531. return 0;
  532. size = i_size_read(inode);
  533. if (offset < size)
  534. filemap_write_and_wait_range(mapping, offset,
  535. offset + count - 1);
  536. return v9fs_file_read(filp, udata, count, offsetp);
  537. }
  538. /**
  539. * v9fs_cached_file_read - read from a file
  540. * @filp: file pointer to read
  541. * @udata: user data buffer to read data into
  542. * @count: size of buffer
  543. * @offset: offset at which to read data
  544. *
  545. */
  546. static ssize_t
  547. v9fs_cached_file_read(struct file *filp, char __user *data, size_t count,
  548. loff_t *offset)
  549. {
  550. if (filp->f_flags & O_DIRECT)
  551. return v9fs_direct_read(filp, data, count, offset);
  552. return do_sync_read(filp, data, count, offset);
  553. }
  554. static ssize_t
  555. v9fs_direct_write(struct file *filp, const char __user * data,
  556. size_t count, loff_t *offsetp)
  557. {
  558. loff_t offset;
  559. ssize_t retval;
  560. struct inode *inode;
  561. struct address_space *mapping;
  562. offset = *offsetp;
  563. mapping = filp->f_mapping;
  564. inode = mapping->host;
  565. if (!count)
  566. return 0;
  567. mutex_lock(&inode->i_mutex);
  568. retval = filemap_write_and_wait_range(mapping, offset,
  569. offset + count - 1);
  570. if (retval)
  571. goto err_out;
  572. /*
  573. * After a write we want buffered reads to be sure to go to disk to get
  574. * the new data. We invalidate clean cached page from the region we're
  575. * about to write. We do this *before* the write so that if we fail
  576. * here we fall back to buffered write
  577. */
  578. if (mapping->nrpages) {
  579. pgoff_t pg_start = offset >> PAGE_CACHE_SHIFT;
  580. pgoff_t pg_end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
  581. retval = invalidate_inode_pages2_range(mapping,
  582. pg_start, pg_end);
  583. /*
  584. * If a page can not be invalidated, fall back
  585. * to buffered write.
  586. */
  587. if (retval) {
  588. if (retval == -EBUSY)
  589. goto buff_write;
  590. goto err_out;
  591. }
  592. }
  593. retval = v9fs_file_write(filp, data, count, offsetp);
  594. err_out:
  595. mutex_unlock(&inode->i_mutex);
  596. return retval;
  597. buff_write:
  598. mutex_unlock(&inode->i_mutex);
  599. return do_sync_write(filp, data, count, offsetp);
  600. }
  601. /**
  602. * v9fs_cached_file_write - write to a file
  603. * @filp: file pointer to write
  604. * @data: data buffer to write data from
  605. * @count: size of buffer
  606. * @offset: offset at which to write data
  607. *
  608. */
  609. static ssize_t
  610. v9fs_cached_file_write(struct file *filp, const char __user * data,
  611. size_t count, loff_t *offset)
  612. {
  613. if (filp->f_flags & O_DIRECT)
  614. return v9fs_direct_write(filp, data, count, offset);
  615. return do_sync_write(filp, data, count, offset);
  616. }
  617. static const struct vm_operations_struct v9fs_file_vm_ops = {
  618. .fault = filemap_fault,
  619. .page_mkwrite = v9fs_vm_page_mkwrite,
  620. };
  621. const struct file_operations v9fs_cached_file_operations = {
  622. .llseek = generic_file_llseek,
  623. .read = v9fs_cached_file_read,
  624. .write = v9fs_cached_file_write,
  625. .aio_read = generic_file_aio_read,
  626. .aio_write = generic_file_aio_write,
  627. .open = v9fs_file_open,
  628. .release = v9fs_dir_release,
  629. .lock = v9fs_file_lock,
  630. .mmap = v9fs_file_mmap,
  631. .fsync = v9fs_file_fsync,
  632. };
  633. const struct file_operations v9fs_cached_file_operations_dotl = {
  634. .llseek = generic_file_llseek,
  635. .read = v9fs_cached_file_read,
  636. .write = v9fs_cached_file_write,
  637. .aio_read = generic_file_aio_read,
  638. .aio_write = generic_file_aio_write,
  639. .open = v9fs_file_open,
  640. .release = v9fs_dir_release,
  641. .lock = v9fs_file_lock_dotl,
  642. .flock = v9fs_file_flock_dotl,
  643. .mmap = v9fs_file_mmap,
  644. .fsync = v9fs_file_fsync_dotl,
  645. };
  646. const struct file_operations v9fs_file_operations = {
  647. .llseek = generic_file_llseek,
  648. .read = v9fs_file_read,
  649. .write = v9fs_file_write,
  650. .open = v9fs_file_open,
  651. .release = v9fs_dir_release,
  652. .lock = v9fs_file_lock,
  653. .mmap = generic_file_readonly_mmap,
  654. .fsync = v9fs_file_fsync,
  655. };
  656. const struct file_operations v9fs_file_operations_dotl = {
  657. .llseek = generic_file_llseek,
  658. .read = v9fs_file_read,
  659. .write = v9fs_file_write,
  660. .open = v9fs_file_open,
  661. .release = v9fs_dir_release,
  662. .lock = v9fs_file_lock_dotl,
  663. .flock = v9fs_file_flock_dotl,
  664. .mmap = generic_file_readonly_mmap,
  665. .fsync = v9fs_file_fsync_dotl,
  666. };