vfs_file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 = v9fs_open_to_dotl_flags(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. /* map the lock type */
  154. switch (fl->fl_type) {
  155. case F_RDLCK:
  156. flock.type = P9_LOCK_TYPE_RDLCK;
  157. break;
  158. case F_WRLCK:
  159. flock.type = P9_LOCK_TYPE_WRLCK;
  160. break;
  161. case F_UNLCK:
  162. flock.type = P9_LOCK_TYPE_UNLCK;
  163. break;
  164. }
  165. flock.start = fl->fl_start;
  166. if (fl->fl_end == OFFSET_MAX)
  167. flock.length = 0;
  168. else
  169. flock.length = fl->fl_end - fl->fl_start + 1;
  170. flock.proc_id = fl->fl_pid;
  171. flock.client_id = utsname()->nodename;
  172. if (IS_SETLKW(cmd))
  173. flock.flags = P9_LOCK_FLAGS_BLOCK;
  174. /*
  175. * if its a blocked request and we get P9_LOCK_BLOCKED as the status
  176. * for lock request, keep on trying
  177. */
  178. for (;;) {
  179. res = p9_client_lock_dotl(fid, &flock, &status);
  180. if (res < 0)
  181. break;
  182. if (status != P9_LOCK_BLOCKED)
  183. break;
  184. if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
  185. break;
  186. schedule_timeout_interruptible(P9_LOCK_TIMEOUT);
  187. }
  188. /* map 9p status to VFS status */
  189. switch (status) {
  190. case P9_LOCK_SUCCESS:
  191. res = 0;
  192. break;
  193. case P9_LOCK_BLOCKED:
  194. res = -EAGAIN;
  195. break;
  196. case P9_LOCK_ERROR:
  197. case P9_LOCK_GRACE:
  198. res = -ENOLCK;
  199. break;
  200. default:
  201. BUG();
  202. }
  203. /*
  204. * incase server returned error for lock request, revert
  205. * it locally
  206. */
  207. if (res < 0 && fl->fl_type != F_UNLCK) {
  208. fl_type = fl->fl_type;
  209. fl->fl_type = F_UNLCK;
  210. res = posix_lock_file_wait(filp, fl);
  211. fl->fl_type = fl_type;
  212. }
  213. out:
  214. return res;
  215. }
  216. static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
  217. {
  218. struct p9_getlock glock;
  219. struct p9_fid *fid;
  220. int res = 0;
  221. fid = filp->private_data;
  222. BUG_ON(fid == NULL);
  223. posix_test_lock(filp, fl);
  224. /*
  225. * if we have a conflicting lock locally, no need to validate
  226. * with server
  227. */
  228. if (fl->fl_type != F_UNLCK)
  229. return res;
  230. /* convert posix lock to p9 tgetlock args */
  231. memset(&glock, 0, sizeof(glock));
  232. glock.type = P9_LOCK_TYPE_UNLCK;
  233. glock.start = fl->fl_start;
  234. if (fl->fl_end == OFFSET_MAX)
  235. glock.length = 0;
  236. else
  237. glock.length = fl->fl_end - fl->fl_start + 1;
  238. glock.proc_id = fl->fl_pid;
  239. glock.client_id = utsname()->nodename;
  240. res = p9_client_getlock_dotl(fid, &glock);
  241. if (res < 0)
  242. return res;
  243. /* map 9p lock type to os lock type */
  244. switch (glock.type) {
  245. case P9_LOCK_TYPE_RDLCK:
  246. fl->fl_type = F_RDLCK;
  247. break;
  248. case P9_LOCK_TYPE_WRLCK:
  249. fl->fl_type = F_WRLCK;
  250. break;
  251. case P9_LOCK_TYPE_UNLCK:
  252. fl->fl_type = F_UNLCK;
  253. break;
  254. }
  255. if (glock.type != P9_LOCK_TYPE_UNLCK) {
  256. fl->fl_start = glock.start;
  257. if (glock.length == 0)
  258. fl->fl_end = OFFSET_MAX;
  259. else
  260. fl->fl_end = glock.start + glock.length - 1;
  261. fl->fl_pid = glock.proc_id;
  262. }
  263. return res;
  264. }
  265. /**
  266. * v9fs_file_lock_dotl - lock a file (or directory)
  267. * @filp: file to be locked
  268. * @cmd: lock command
  269. * @fl: file lock structure
  270. *
  271. */
  272. static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
  273. {
  274. struct inode *inode = filp->f_path.dentry->d_inode;
  275. int ret = -ENOLCK;
  276. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
  277. cmd, fl, filp->f_path.dentry->d_name.name);
  278. /* No mandatory locks */
  279. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  280. goto out_err;
  281. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  282. filemap_write_and_wait(inode->i_mapping);
  283. invalidate_mapping_pages(&inode->i_data, 0, -1);
  284. }
  285. if (IS_SETLK(cmd) || IS_SETLKW(cmd))
  286. ret = v9fs_file_do_lock(filp, cmd, fl);
  287. else if (IS_GETLK(cmd))
  288. ret = v9fs_file_getlock(filp, fl);
  289. else
  290. ret = -EINVAL;
  291. out_err:
  292. return ret;
  293. }
  294. /**
  295. * v9fs_file_flock_dotl - lock a file
  296. * @filp: file to be locked
  297. * @cmd: lock command
  298. * @fl: file lock structure
  299. *
  300. */
  301. static int v9fs_file_flock_dotl(struct file *filp, int cmd,
  302. struct file_lock *fl)
  303. {
  304. struct inode *inode = filp->f_path.dentry->d_inode;
  305. int ret = -ENOLCK;
  306. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
  307. cmd, fl, filp->f_path.dentry->d_name.name);
  308. /* No mandatory locks */
  309. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  310. goto out_err;
  311. if (!(fl->fl_flags & FL_FLOCK))
  312. goto out_err;
  313. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  314. filemap_write_and_wait(inode->i_mapping);
  315. invalidate_mapping_pages(&inode->i_data, 0, -1);
  316. }
  317. /* Convert flock to posix lock */
  318. fl->fl_owner = (fl_owner_t)filp;
  319. fl->fl_start = 0;
  320. fl->fl_end = OFFSET_MAX;
  321. fl->fl_flags |= FL_POSIX;
  322. fl->fl_flags ^= FL_FLOCK;
  323. if (IS_SETLK(cmd) | IS_SETLKW(cmd))
  324. ret = v9fs_file_do_lock(filp, cmd, fl);
  325. else
  326. ret = -EINVAL;
  327. out_err:
  328. return ret;
  329. }
  330. /**
  331. * v9fs_fid_readn - read from a fid
  332. * @fid: fid to read
  333. * @data: data buffer to read data into
  334. * @udata: user data buffer to read data into
  335. * @count: size of buffer
  336. * @offset: offset at which to read data
  337. *
  338. */
  339. ssize_t
  340. v9fs_fid_readn(struct p9_fid *fid, char *data, char __user *udata, u32 count,
  341. u64 offset)
  342. {
  343. int n, total, size;
  344. P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
  345. (long long unsigned) offset, count);
  346. n = 0;
  347. total = 0;
  348. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  349. do {
  350. n = p9_client_read(fid, data, udata, offset, count);
  351. if (n <= 0)
  352. break;
  353. if (data)
  354. data += n;
  355. if (udata)
  356. udata += n;
  357. offset += n;
  358. count -= n;
  359. total += n;
  360. } while (count > 0 && n == size);
  361. if (n < 0)
  362. total = n;
  363. return total;
  364. }
  365. /**
  366. * v9fs_file_readn - read from a file
  367. * @filp: file pointer to read
  368. * @data: data buffer to read data into
  369. * @udata: user data buffer to read data into
  370. * @count: size of buffer
  371. * @offset: offset at which to read data
  372. *
  373. */
  374. ssize_t
  375. v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
  376. u64 offset)
  377. {
  378. return v9fs_fid_readn(filp->private_data, data, udata, count, offset);
  379. }
  380. /**
  381. * v9fs_file_read - read from a file
  382. * @filp: file pointer to read
  383. * @udata: user data buffer to read data into
  384. * @count: size of buffer
  385. * @offset: offset at which to read data
  386. *
  387. */
  388. static ssize_t
  389. v9fs_file_read(struct file *filp, char __user *udata, size_t count,
  390. loff_t * offset)
  391. {
  392. int ret;
  393. struct p9_fid *fid;
  394. size_t size;
  395. P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
  396. fid = filp->private_data;
  397. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  398. if (count > size)
  399. ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
  400. else
  401. ret = p9_client_read(fid, NULL, udata, *offset, count);
  402. if (ret > 0)
  403. *offset += ret;
  404. return ret;
  405. }
  406. ssize_t
  407. v9fs_file_write_internal(struct inode *inode, struct p9_fid *fid,
  408. const char __user *data, size_t count,
  409. loff_t *offset, int invalidate)
  410. {
  411. int n;
  412. loff_t i_size;
  413. size_t total = 0;
  414. struct p9_client *clnt;
  415. loff_t origin = *offset;
  416. unsigned long pg_start, pg_end;
  417. P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
  418. (int)count, (int)*offset);
  419. clnt = fid->clnt;
  420. do {
  421. n = p9_client_write(fid, NULL, data+total, origin+total, count);
  422. if (n <= 0)
  423. break;
  424. count -= n;
  425. total += n;
  426. } while (count > 0);
  427. if (invalidate && (total > 0)) {
  428. pg_start = origin >> PAGE_CACHE_SHIFT;
  429. pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
  430. if (inode->i_mapping && inode->i_mapping->nrpages)
  431. invalidate_inode_pages2_range(inode->i_mapping,
  432. pg_start, pg_end);
  433. *offset += total;
  434. i_size = i_size_read(inode);
  435. if (*offset > i_size) {
  436. inode_add_bytes(inode, *offset - i_size);
  437. i_size_write(inode, *offset);
  438. }
  439. }
  440. if (n < 0)
  441. return n;
  442. return total;
  443. }
  444. /**
  445. * v9fs_file_write - write to a file
  446. * @filp: file pointer to write
  447. * @data: data buffer to write data from
  448. * @count: size of buffer
  449. * @offset: offset at which to write data
  450. *
  451. */
  452. static ssize_t
  453. v9fs_file_write(struct file *filp, const char __user * data,
  454. size_t count, loff_t *offset)
  455. {
  456. ssize_t retval = 0;
  457. loff_t origin = *offset;
  458. retval = generic_write_checks(filp, &origin, &count, 0);
  459. if (retval)
  460. goto out;
  461. retval = -EINVAL;
  462. if ((ssize_t) count < 0)
  463. goto out;
  464. retval = 0;
  465. if (!count)
  466. goto out;
  467. retval = v9fs_file_write_internal(filp->f_path.dentry->d_inode,
  468. filp->private_data,
  469. data, count, &origin, 1);
  470. /* update offset on successful write */
  471. if (retval > 0)
  472. *offset = origin;
  473. out:
  474. return retval;
  475. }
  476. static int v9fs_file_fsync(struct file *filp, int datasync)
  477. {
  478. struct p9_fid *fid;
  479. struct p9_wstat wstat;
  480. int retval;
  481. P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
  482. fid = filp->private_data;
  483. v9fs_blank_wstat(&wstat);
  484. retval = p9_client_wstat(fid, &wstat);
  485. return retval;
  486. }
  487. int v9fs_file_fsync_dotl(struct file *filp, int datasync)
  488. {
  489. struct p9_fid *fid;
  490. int retval;
  491. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_file_fsync_dotl: filp %p datasync %x\n",
  492. filp, datasync);
  493. fid = filp->private_data;
  494. retval = p9_client_fsync(fid, datasync);
  495. return retval;
  496. }
  497. static int
  498. v9fs_file_mmap(struct file *file, struct vm_area_struct *vma)
  499. {
  500. int retval;
  501. retval = generic_file_mmap(file, vma);
  502. if (!retval)
  503. vma->vm_ops = &v9fs_file_vm_ops;
  504. return retval;
  505. }
  506. static int
  507. v9fs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  508. {
  509. struct v9fs_inode *v9inode;
  510. struct page *page = vmf->page;
  511. struct file *filp = vma->vm_file;
  512. struct inode *inode = filp->f_path.dentry->d_inode;
  513. P9_DPRINTK(P9_DEBUG_VFS, "page %p fid %lx\n",
  514. page, (unsigned long)filp->private_data);
  515. v9inode = V9FS_I(inode);
  516. /* make sure the cache has finished storing the page */
  517. v9fs_fscache_wait_on_page_write(inode, page);
  518. BUG_ON(!v9inode->writeback_fid);
  519. lock_page(page);
  520. if (page->mapping != inode->i_mapping)
  521. goto out_unlock;
  522. return VM_FAULT_LOCKED;
  523. out_unlock:
  524. unlock_page(page);
  525. return VM_FAULT_NOPAGE;
  526. }
  527. static ssize_t
  528. v9fs_direct_read(struct file *filp, char __user *udata, size_t count,
  529. loff_t *offsetp)
  530. {
  531. loff_t size, offset;
  532. struct inode *inode;
  533. struct address_space *mapping;
  534. offset = *offsetp;
  535. mapping = filp->f_mapping;
  536. inode = mapping->host;
  537. if (!count)
  538. return 0;
  539. size = i_size_read(inode);
  540. if (offset < size)
  541. filemap_write_and_wait_range(mapping, offset,
  542. offset + count - 1);
  543. return v9fs_file_read(filp, udata, count, offsetp);
  544. }
  545. /**
  546. * v9fs_cached_file_read - read from a file
  547. * @filp: file pointer to read
  548. * @udata: user data buffer to read data into
  549. * @count: size of buffer
  550. * @offset: offset at which to read data
  551. *
  552. */
  553. static ssize_t
  554. v9fs_cached_file_read(struct file *filp, char __user *data, size_t count,
  555. loff_t *offset)
  556. {
  557. if (filp->f_flags & O_DIRECT)
  558. return v9fs_direct_read(filp, data, count, offset);
  559. return do_sync_read(filp, data, count, offset);
  560. }
  561. static ssize_t
  562. v9fs_direct_write(struct file *filp, const char __user * data,
  563. size_t count, loff_t *offsetp)
  564. {
  565. loff_t offset;
  566. ssize_t retval;
  567. struct inode *inode;
  568. struct address_space *mapping;
  569. offset = *offsetp;
  570. mapping = filp->f_mapping;
  571. inode = mapping->host;
  572. if (!count)
  573. return 0;
  574. mutex_lock(&inode->i_mutex);
  575. retval = filemap_write_and_wait_range(mapping, offset,
  576. offset + count - 1);
  577. if (retval)
  578. goto err_out;
  579. /*
  580. * After a write we want buffered reads to be sure to go to disk to get
  581. * the new data. We invalidate clean cached page from the region we're
  582. * about to write. We do this *before* the write so that if we fail
  583. * here we fall back to buffered write
  584. */
  585. if (mapping->nrpages) {
  586. pgoff_t pg_start = offset >> PAGE_CACHE_SHIFT;
  587. pgoff_t pg_end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
  588. retval = invalidate_inode_pages2_range(mapping,
  589. pg_start, pg_end);
  590. /*
  591. * If a page can not be invalidated, fall back
  592. * to buffered write.
  593. */
  594. if (retval) {
  595. if (retval == -EBUSY)
  596. goto buff_write;
  597. goto err_out;
  598. }
  599. }
  600. retval = v9fs_file_write(filp, data, count, offsetp);
  601. err_out:
  602. mutex_unlock(&inode->i_mutex);
  603. return retval;
  604. buff_write:
  605. mutex_unlock(&inode->i_mutex);
  606. return do_sync_write(filp, data, count, offsetp);
  607. }
  608. /**
  609. * v9fs_cached_file_write - write to a file
  610. * @filp: file pointer to write
  611. * @data: data buffer to write data from
  612. * @count: size of buffer
  613. * @offset: offset at which to write data
  614. *
  615. */
  616. static ssize_t
  617. v9fs_cached_file_write(struct file *filp, const char __user * data,
  618. size_t count, loff_t *offset)
  619. {
  620. if (filp->f_flags & O_DIRECT)
  621. return v9fs_direct_write(filp, data, count, offset);
  622. return do_sync_write(filp, data, count, offset);
  623. }
  624. static const struct vm_operations_struct v9fs_file_vm_ops = {
  625. .fault = filemap_fault,
  626. .page_mkwrite = v9fs_vm_page_mkwrite,
  627. };
  628. const struct file_operations v9fs_cached_file_operations = {
  629. .llseek = generic_file_llseek,
  630. .read = v9fs_cached_file_read,
  631. .write = v9fs_cached_file_write,
  632. .aio_read = generic_file_aio_read,
  633. .aio_write = generic_file_aio_write,
  634. .open = v9fs_file_open,
  635. .release = v9fs_dir_release,
  636. .lock = v9fs_file_lock,
  637. .mmap = v9fs_file_mmap,
  638. .fsync = v9fs_file_fsync,
  639. };
  640. const struct file_operations v9fs_cached_file_operations_dotl = {
  641. .llseek = generic_file_llseek,
  642. .read = v9fs_cached_file_read,
  643. .write = v9fs_cached_file_write,
  644. .aio_read = generic_file_aio_read,
  645. .aio_write = generic_file_aio_write,
  646. .open = v9fs_file_open,
  647. .release = v9fs_dir_release,
  648. .lock = v9fs_file_lock_dotl,
  649. .flock = v9fs_file_flock_dotl,
  650. .mmap = v9fs_file_mmap,
  651. .fsync = v9fs_file_fsync_dotl,
  652. };
  653. const struct file_operations v9fs_file_operations = {
  654. .llseek = generic_file_llseek,
  655. .read = v9fs_file_read,
  656. .write = v9fs_file_write,
  657. .open = v9fs_file_open,
  658. .release = v9fs_dir_release,
  659. .lock = v9fs_file_lock,
  660. .mmap = generic_file_readonly_mmap,
  661. .fsync = v9fs_file_fsync,
  662. };
  663. const struct file_operations v9fs_file_operations_dotl = {
  664. .llseek = generic_file_llseek,
  665. .read = v9fs_file_read,
  666. .write = v9fs_file_write,
  667. .open = v9fs_file_open,
  668. .release = v9fs_dir_release,
  669. .lock = v9fs_file_lock_dotl,
  670. .flock = v9fs_file_flock_dotl,
  671. .mmap = generic_file_readonly_mmap,
  672. .fsync = v9fs_file_fsync_dotl,
  673. };