vfs_file.c 18 KB

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