vfs_file.c 18 KB

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