vfs_file.c 18 KB

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