vfs_file.c 12 KB

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