vfs_file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. /**
  45. * v9fs_file_open - open a file (or directory)
  46. * @inode: inode to be opened
  47. * @file: file being opened
  48. *
  49. */
  50. int v9fs_file_open(struct inode *inode, struct file *file)
  51. {
  52. int err;
  53. struct v9fs_session_info *v9ses;
  54. struct p9_fid *fid;
  55. int omode;
  56. P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
  57. v9ses = v9fs_inode2v9ses(inode);
  58. if (v9fs_proto_dotl(v9ses))
  59. omode = file->f_flags;
  60. else
  61. omode = v9fs_uflags2omode(file->f_flags,
  62. v9fs_proto_dotu(v9ses));
  63. fid = file->private_data;
  64. if (!fid) {
  65. fid = v9fs_fid_clone(file->f_path.dentry);
  66. if (IS_ERR(fid))
  67. return PTR_ERR(fid);
  68. err = p9_client_open(fid, omode);
  69. if (err < 0) {
  70. p9_client_clunk(fid);
  71. return err;
  72. }
  73. if (file->f_flags & O_TRUNC) {
  74. i_size_write(inode, 0);
  75. inode->i_blocks = 0;
  76. }
  77. if ((file->f_flags & O_APPEND) &&
  78. (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
  79. generic_file_llseek(file, 0, SEEK_END);
  80. }
  81. file->private_data = fid;
  82. if (v9ses->cache && !inode->i_private) {
  83. /*
  84. * clone a fid and add it to inode->i_private
  85. * we do it during open time instead of
  86. * page dirty time via write_begin/page_mkwrite
  87. * because we want write after unlink usecase
  88. * to work.
  89. */
  90. fid = v9fs_writeback_fid(file->f_path.dentry);
  91. if (IS_ERR(fid)) {
  92. err = PTR_ERR(fid);
  93. goto out_error;
  94. }
  95. inode->i_private = (void *) fid;
  96. }
  97. #ifdef CONFIG_9P_FSCACHE
  98. if (v9ses->cache)
  99. v9fs_cache_inode_set_cookie(inode, file);
  100. #endif
  101. return 0;
  102. out_error:
  103. p9_client_clunk(file->private_data);
  104. file->private_data = NULL;
  105. return err;
  106. }
  107. /**
  108. * v9fs_file_lock - lock a file (or directory)
  109. * @filp: file to be locked
  110. * @cmd: lock command
  111. * @fl: file lock structure
  112. *
  113. * Bugs: this looks like a local only lock, we should extend into 9P
  114. * by using open exclusive
  115. */
  116. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  117. {
  118. int res = 0;
  119. struct inode *inode = filp->f_path.dentry->d_inode;
  120. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  121. /* No mandatory locks */
  122. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  123. return -ENOLCK;
  124. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  125. filemap_write_and_wait(inode->i_mapping);
  126. invalidate_mapping_pages(&inode->i_data, 0, -1);
  127. }
  128. return res;
  129. }
  130. static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
  131. {
  132. struct p9_flock flock;
  133. struct p9_fid *fid;
  134. uint8_t status;
  135. int res = 0;
  136. unsigned char fl_type;
  137. fid = filp->private_data;
  138. BUG_ON(fid == NULL);
  139. if ((fl->fl_flags & FL_POSIX) != FL_POSIX)
  140. BUG();
  141. res = posix_lock_file_wait(filp, fl);
  142. if (res < 0)
  143. goto out;
  144. /* convert posix lock to p9 tlock args */
  145. memset(&flock, 0, sizeof(flock));
  146. flock.type = fl->fl_type;
  147. flock.start = fl->fl_start;
  148. if (fl->fl_end == OFFSET_MAX)
  149. flock.length = 0;
  150. else
  151. flock.length = fl->fl_end - fl->fl_start + 1;
  152. flock.proc_id = fl->fl_pid;
  153. flock.client_id = utsname()->nodename;
  154. if (IS_SETLKW(cmd))
  155. flock.flags = P9_LOCK_FLAGS_BLOCK;
  156. /*
  157. * if its a blocked request and we get P9_LOCK_BLOCKED as the status
  158. * for lock request, keep on trying
  159. */
  160. for (;;) {
  161. res = p9_client_lock_dotl(fid, &flock, &status);
  162. if (res < 0)
  163. break;
  164. if (status != P9_LOCK_BLOCKED)
  165. break;
  166. if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
  167. break;
  168. schedule_timeout_interruptible(P9_LOCK_TIMEOUT);
  169. }
  170. /* map 9p status to VFS status */
  171. switch (status) {
  172. case P9_LOCK_SUCCESS:
  173. res = 0;
  174. break;
  175. case P9_LOCK_BLOCKED:
  176. res = -EAGAIN;
  177. break;
  178. case P9_LOCK_ERROR:
  179. case P9_LOCK_GRACE:
  180. res = -ENOLCK;
  181. break;
  182. default:
  183. BUG();
  184. }
  185. /*
  186. * incase server returned error for lock request, revert
  187. * it locally
  188. */
  189. if (res < 0 && fl->fl_type != F_UNLCK) {
  190. fl_type = fl->fl_type;
  191. fl->fl_type = F_UNLCK;
  192. res = posix_lock_file_wait(filp, fl);
  193. fl->fl_type = fl_type;
  194. }
  195. out:
  196. return res;
  197. }
  198. static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
  199. {
  200. struct p9_getlock glock;
  201. struct p9_fid *fid;
  202. int res = 0;
  203. fid = filp->private_data;
  204. BUG_ON(fid == NULL);
  205. posix_test_lock(filp, fl);
  206. /*
  207. * if we have a conflicting lock locally, no need to validate
  208. * with server
  209. */
  210. if (fl->fl_type != F_UNLCK)
  211. return res;
  212. /* convert posix lock to p9 tgetlock args */
  213. memset(&glock, 0, sizeof(glock));
  214. glock.type = fl->fl_type;
  215. glock.start = fl->fl_start;
  216. if (fl->fl_end == OFFSET_MAX)
  217. glock.length = 0;
  218. else
  219. glock.length = fl->fl_end - fl->fl_start + 1;
  220. glock.proc_id = fl->fl_pid;
  221. glock.client_id = utsname()->nodename;
  222. res = p9_client_getlock_dotl(fid, &glock);
  223. if (res < 0)
  224. return res;
  225. if (glock.type != F_UNLCK) {
  226. fl->fl_type = glock.type;
  227. fl->fl_start = glock.start;
  228. if (glock.length == 0)
  229. fl->fl_end = OFFSET_MAX;
  230. else
  231. fl->fl_end = glock.start + glock.length - 1;
  232. fl->fl_pid = glock.proc_id;
  233. } else
  234. fl->fl_type = F_UNLCK;
  235. return res;
  236. }
  237. /**
  238. * v9fs_file_lock_dotl - lock a file (or directory)
  239. * @filp: file to be locked
  240. * @cmd: lock command
  241. * @fl: file lock structure
  242. *
  243. */
  244. static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
  245. {
  246. struct inode *inode = filp->f_path.dentry->d_inode;
  247. int ret = -ENOLCK;
  248. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
  249. cmd, fl, filp->f_path.dentry->d_name.name);
  250. /* No mandatory locks */
  251. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  252. goto out_err;
  253. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  254. filemap_write_and_wait(inode->i_mapping);
  255. invalidate_mapping_pages(&inode->i_data, 0, -1);
  256. }
  257. if (IS_SETLK(cmd) || IS_SETLKW(cmd))
  258. ret = v9fs_file_do_lock(filp, cmd, fl);
  259. else if (IS_GETLK(cmd))
  260. ret = v9fs_file_getlock(filp, fl);
  261. else
  262. ret = -EINVAL;
  263. out_err:
  264. return ret;
  265. }
  266. /**
  267. * v9fs_file_flock_dotl - lock a file
  268. * @filp: file to be locked
  269. * @cmd: lock command
  270. * @fl: file lock structure
  271. *
  272. */
  273. static int v9fs_file_flock_dotl(struct file *filp, int cmd,
  274. struct file_lock *fl)
  275. {
  276. struct inode *inode = filp->f_path.dentry->d_inode;
  277. int ret = -ENOLCK;
  278. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
  279. cmd, fl, filp->f_path.dentry->d_name.name);
  280. /* No mandatory locks */
  281. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  282. goto out_err;
  283. if (!(fl->fl_flags & FL_FLOCK))
  284. goto out_err;
  285. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  286. filemap_write_and_wait(inode->i_mapping);
  287. invalidate_mapping_pages(&inode->i_data, 0, -1);
  288. }
  289. /* Convert flock to posix lock */
  290. fl->fl_owner = (fl_owner_t)filp;
  291. fl->fl_start = 0;
  292. fl->fl_end = OFFSET_MAX;
  293. fl->fl_flags |= FL_POSIX;
  294. fl->fl_flags ^= FL_FLOCK;
  295. if (IS_SETLK(cmd) | IS_SETLKW(cmd))
  296. ret = v9fs_file_do_lock(filp, cmd, fl);
  297. else
  298. ret = -EINVAL;
  299. out_err:
  300. return ret;
  301. }
  302. /**
  303. * v9fs_fid_readn - read from a fid
  304. * @fid: fid to read
  305. * @data: data buffer to read data into
  306. * @udata: user data buffer to read data into
  307. * @count: size of buffer
  308. * @offset: offset at which to read data
  309. *
  310. */
  311. ssize_t
  312. v9fs_fid_readn(struct p9_fid *fid, char *data, char __user *udata, u32 count,
  313. u64 offset)
  314. {
  315. int n, total, size;
  316. P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
  317. (long long unsigned) offset, count);
  318. n = 0;
  319. total = 0;
  320. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  321. do {
  322. n = p9_client_read(fid, data, udata, offset, count);
  323. if (n <= 0)
  324. break;
  325. if (data)
  326. data += n;
  327. if (udata)
  328. udata += n;
  329. offset += n;
  330. count -= n;
  331. total += n;
  332. } while (count > 0 && n == size);
  333. if (n < 0)
  334. total = n;
  335. return total;
  336. }
  337. /**
  338. * v9fs_file_readn - read from a file
  339. * @filp: file pointer to read
  340. * @data: data buffer to read data into
  341. * @udata: user data buffer to read data into
  342. * @count: size of buffer
  343. * @offset: offset at which to read data
  344. *
  345. */
  346. ssize_t
  347. v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
  348. u64 offset)
  349. {
  350. return v9fs_fid_readn(filp->private_data, data, udata, count, offset);
  351. }
  352. /**
  353. * v9fs_file_read - read from a file
  354. * @filp: file pointer to read
  355. * @udata: user data buffer to read data into
  356. * @count: size of buffer
  357. * @offset: offset at which to read data
  358. *
  359. */
  360. static ssize_t
  361. v9fs_file_read(struct file *filp, char __user *udata, size_t count,
  362. loff_t * offset)
  363. {
  364. int ret;
  365. struct p9_fid *fid;
  366. size_t size;
  367. P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
  368. fid = filp->private_data;
  369. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  370. if (count > size)
  371. ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
  372. else
  373. ret = p9_client_read(fid, NULL, udata, *offset, count);
  374. if (ret > 0)
  375. *offset += ret;
  376. return ret;
  377. }
  378. ssize_t
  379. v9fs_file_write_internal(struct inode *inode, struct p9_fid *fid,
  380. const char __user *data, size_t count,
  381. loff_t *offset, int invalidate)
  382. {
  383. int n;
  384. size_t total = 0;
  385. struct p9_client *clnt;
  386. loff_t origin = *offset;
  387. unsigned long pg_start, pg_end;
  388. P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
  389. (int)count, (int)*offset);
  390. clnt = fid->clnt;
  391. do {
  392. n = p9_client_write(fid, NULL, data+total, origin+total, count);
  393. if (n <= 0)
  394. break;
  395. count -= n;
  396. total += n;
  397. } while (count > 0);
  398. if (invalidate && (total > 0)) {
  399. pg_start = origin >> PAGE_CACHE_SHIFT;
  400. pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
  401. if (inode->i_mapping && inode->i_mapping->nrpages)
  402. invalidate_inode_pages2_range(inode->i_mapping,
  403. pg_start, pg_end);
  404. *offset += total;
  405. i_size_write(inode, i_size_read(inode) + total);
  406. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  407. }
  408. if (n < 0)
  409. return n;
  410. return total;
  411. }
  412. /**
  413. * v9fs_file_write - write to a file
  414. * @filp: file pointer to write
  415. * @data: data buffer to write data from
  416. * @count: size of buffer
  417. * @offset: offset at which to write data
  418. *
  419. */
  420. static ssize_t
  421. v9fs_file_write(struct file *filp, const char __user * data,
  422. size_t count, loff_t *offset)
  423. {
  424. ssize_t retval = 0;
  425. loff_t origin = *offset;
  426. retval = generic_write_checks(filp, &origin, &count, 0);
  427. if (retval)
  428. goto out;
  429. retval = -EINVAL;
  430. if ((ssize_t) count < 0)
  431. goto out;
  432. retval = 0;
  433. if (!count)
  434. goto out;
  435. return v9fs_file_write_internal(filp->f_path.dentry->d_inode,
  436. filp->private_data,
  437. data, count, offset, 1);
  438. out:
  439. return retval;
  440. }
  441. static int v9fs_file_fsync(struct file *filp, int datasync)
  442. {
  443. struct p9_fid *fid;
  444. struct p9_wstat wstat;
  445. int retval;
  446. P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
  447. fid = filp->private_data;
  448. v9fs_blank_wstat(&wstat);
  449. retval = p9_client_wstat(fid, &wstat);
  450. return retval;
  451. }
  452. int v9fs_file_fsync_dotl(struct file *filp, int datasync)
  453. {
  454. struct p9_fid *fid;
  455. int retval;
  456. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_file_fsync_dotl: filp %p datasync %x\n",
  457. filp, datasync);
  458. fid = filp->private_data;
  459. retval = p9_client_fsync(fid, datasync);
  460. return retval;
  461. }
  462. const struct file_operations v9fs_cached_file_operations = {
  463. .llseek = generic_file_llseek,
  464. .read = do_sync_read,
  465. .aio_read = generic_file_aio_read,
  466. .write = v9fs_file_write,
  467. .open = v9fs_file_open,
  468. .release = v9fs_dir_release,
  469. .lock = v9fs_file_lock,
  470. .mmap = generic_file_readonly_mmap,
  471. .fsync = v9fs_file_fsync,
  472. };
  473. const struct file_operations v9fs_cached_file_operations_dotl = {
  474. .llseek = generic_file_llseek,
  475. .read = do_sync_read,
  476. .aio_read = generic_file_aio_read,
  477. .write = v9fs_file_write,
  478. .open = v9fs_file_open,
  479. .release = v9fs_dir_release,
  480. .lock = v9fs_file_lock_dotl,
  481. .flock = v9fs_file_flock_dotl,
  482. .mmap = generic_file_readonly_mmap,
  483. .fsync = v9fs_file_fsync_dotl,
  484. };
  485. const struct file_operations v9fs_file_operations = {
  486. .llseek = generic_file_llseek,
  487. .read = v9fs_file_read,
  488. .write = v9fs_file_write,
  489. .open = v9fs_file_open,
  490. .release = v9fs_dir_release,
  491. .lock = v9fs_file_lock,
  492. .mmap = generic_file_readonly_mmap,
  493. .fsync = v9fs_file_fsync,
  494. };
  495. const struct file_operations v9fs_file_operations_dotl = {
  496. .llseek = generic_file_llseek,
  497. .read = v9fs_file_read,
  498. .write = v9fs_file_write,
  499. .open = v9fs_file_open,
  500. .release = v9fs_dir_release,
  501. .lock = v9fs_file_lock_dotl,
  502. .flock = v9fs_file_flock_dotl,
  503. .mmap = generic_file_readonly_mmap,
  504. .fsync = v9fs_file_fsync_dotl,
  505. };