vfs_file.c 13 KB

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