vfs_file.c 12 KB

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