vfs_inode_dotl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /*
  2. * linux/fs/9p/vfs_inode_dotl.c
  3. *
  4. * This file contains vfs inode ops for the 9P2000.L protocol.
  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/file.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/inet.h>
  33. #include <linux/namei.h>
  34. #include <linux/idr.h>
  35. #include <linux/sched.h>
  36. #include <linux/slab.h>
  37. #include <linux/xattr.h>
  38. #include <linux/posix_acl.h>
  39. #include <net/9p/9p.h>
  40. #include <net/9p/client.h>
  41. #include "v9fs.h"
  42. #include "v9fs_vfs.h"
  43. #include "fid.h"
  44. #include "cache.h"
  45. #include "xattr.h"
  46. #include "acl.h"
  47. static int
  48. v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
  49. dev_t rdev);
  50. /**
  51. * v9fs_get_fsgid_for_create - Helper function to get the gid for creating a
  52. * new file system object. This checks the S_ISGID to determine the owning
  53. * group of the new file system object.
  54. */
  55. static kgid_t v9fs_get_fsgid_for_create(struct inode *dir_inode)
  56. {
  57. BUG_ON(dir_inode == NULL);
  58. if (dir_inode->i_mode & S_ISGID) {
  59. /* set_gid bit is set.*/
  60. return dir_inode->i_gid;
  61. }
  62. return current_fsgid();
  63. }
  64. static int v9fs_test_inode_dotl(struct inode *inode, void *data)
  65. {
  66. struct v9fs_inode *v9inode = V9FS_I(inode);
  67. struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
  68. /* don't match inode of different type */
  69. if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
  70. return 0;
  71. if (inode->i_generation != st->st_gen)
  72. return 0;
  73. /* compare qid details */
  74. if (memcmp(&v9inode->qid.version,
  75. &st->qid.version, sizeof(v9inode->qid.version)))
  76. return 0;
  77. if (v9inode->qid.type != st->qid.type)
  78. return 0;
  79. return 1;
  80. }
  81. /* Always get a new inode */
  82. static int v9fs_test_new_inode_dotl(struct inode *inode, void *data)
  83. {
  84. return 0;
  85. }
  86. static int v9fs_set_inode_dotl(struct inode *inode, void *data)
  87. {
  88. struct v9fs_inode *v9inode = V9FS_I(inode);
  89. struct p9_stat_dotl *st = (struct p9_stat_dotl *)data;
  90. memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
  91. inode->i_generation = st->st_gen;
  92. return 0;
  93. }
  94. static struct inode *v9fs_qid_iget_dotl(struct super_block *sb,
  95. struct p9_qid *qid,
  96. struct p9_fid *fid,
  97. struct p9_stat_dotl *st,
  98. int new)
  99. {
  100. int retval;
  101. unsigned long i_ino;
  102. struct inode *inode;
  103. struct v9fs_session_info *v9ses = sb->s_fs_info;
  104. int (*test)(struct inode *, void *);
  105. if (new)
  106. test = v9fs_test_new_inode_dotl;
  107. else
  108. test = v9fs_test_inode_dotl;
  109. i_ino = v9fs_qid2ino(qid);
  110. inode = iget5_locked(sb, i_ino, test, v9fs_set_inode_dotl, st);
  111. if (!inode)
  112. return ERR_PTR(-ENOMEM);
  113. if (!(inode->i_state & I_NEW))
  114. return inode;
  115. /*
  116. * initialize the inode with the stat info
  117. * FIXME!! we may need support for stale inodes
  118. * later.
  119. */
  120. inode->i_ino = i_ino;
  121. retval = v9fs_init_inode(v9ses, inode,
  122. st->st_mode, new_decode_dev(st->st_rdev));
  123. if (retval)
  124. goto error;
  125. v9fs_stat2inode_dotl(st, inode);
  126. v9fs_cache_inode_get_cookie(inode);
  127. retval = v9fs_get_acl(inode, fid);
  128. if (retval)
  129. goto error;
  130. unlock_new_inode(inode);
  131. return inode;
  132. error:
  133. iget_failed(inode);
  134. return ERR_PTR(retval);
  135. }
  136. struct inode *
  137. v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  138. struct super_block *sb, int new)
  139. {
  140. struct p9_stat_dotl *st;
  141. struct inode *inode = NULL;
  142. st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
  143. if (IS_ERR(st))
  144. return ERR_CAST(st);
  145. inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new);
  146. kfree(st);
  147. return inode;
  148. }
  149. struct dotl_openflag_map {
  150. int open_flag;
  151. int dotl_flag;
  152. };
  153. static int v9fs_mapped_dotl_flags(int flags)
  154. {
  155. int i;
  156. int rflags = 0;
  157. struct dotl_openflag_map dotl_oflag_map[] = {
  158. { O_CREAT, P9_DOTL_CREATE },
  159. { O_EXCL, P9_DOTL_EXCL },
  160. { O_NOCTTY, P9_DOTL_NOCTTY },
  161. { O_APPEND, P9_DOTL_APPEND },
  162. { O_NONBLOCK, P9_DOTL_NONBLOCK },
  163. { O_DSYNC, P9_DOTL_DSYNC },
  164. { FASYNC, P9_DOTL_FASYNC },
  165. { O_DIRECT, P9_DOTL_DIRECT },
  166. { O_LARGEFILE, P9_DOTL_LARGEFILE },
  167. { O_DIRECTORY, P9_DOTL_DIRECTORY },
  168. { O_NOFOLLOW, P9_DOTL_NOFOLLOW },
  169. { O_NOATIME, P9_DOTL_NOATIME },
  170. { O_CLOEXEC, P9_DOTL_CLOEXEC },
  171. { O_SYNC, P9_DOTL_SYNC},
  172. };
  173. for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
  174. if (flags & dotl_oflag_map[i].open_flag)
  175. rflags |= dotl_oflag_map[i].dotl_flag;
  176. }
  177. return rflags;
  178. }
  179. /**
  180. * v9fs_open_to_dotl_flags- convert Linux specific open flags to
  181. * plan 9 open flag.
  182. * @flags: flags to convert
  183. */
  184. int v9fs_open_to_dotl_flags(int flags)
  185. {
  186. int rflags = 0;
  187. /*
  188. * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
  189. * and P9_DOTL_NOACCESS
  190. */
  191. rflags |= flags & O_ACCMODE;
  192. rflags |= v9fs_mapped_dotl_flags(flags);
  193. return rflags;
  194. }
  195. /**
  196. * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
  197. * @dir: directory inode that is being created
  198. * @dentry: dentry that is being deleted
  199. * @omode: create permissions
  200. *
  201. */
  202. static int
  203. v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
  204. bool excl)
  205. {
  206. return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
  207. }
  208. static int
  209. v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry,
  210. struct file *file, unsigned flags, umode_t omode,
  211. int *opened)
  212. {
  213. int err = 0;
  214. kgid_t gid;
  215. umode_t mode;
  216. char *name = NULL;
  217. struct p9_qid qid;
  218. struct inode *inode;
  219. struct p9_fid *fid = NULL;
  220. struct v9fs_inode *v9inode;
  221. struct p9_fid *dfid, *ofid, *inode_fid;
  222. struct v9fs_session_info *v9ses;
  223. struct posix_acl *pacl = NULL, *dacl = NULL;
  224. struct dentry *res = NULL;
  225. if (d_in_lookup(dentry)) {
  226. res = v9fs_vfs_lookup(dir, dentry, 0);
  227. if (IS_ERR(res))
  228. return PTR_ERR(res);
  229. if (res)
  230. dentry = res;
  231. }
  232. /* Only creates */
  233. if (!(flags & O_CREAT) || d_really_is_positive(dentry))
  234. return finish_no_open(file, res);
  235. v9ses = v9fs_inode2v9ses(dir);
  236. name = (char *) dentry->d_name.name;
  237. p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%hx\n",
  238. name, flags, omode);
  239. dfid = v9fs_parent_fid(dentry);
  240. if (IS_ERR(dfid)) {
  241. err = PTR_ERR(dfid);
  242. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  243. goto out;
  244. }
  245. /* clone a fid to use for creation */
  246. ofid = clone_fid(dfid);
  247. if (IS_ERR(ofid)) {
  248. err = PTR_ERR(ofid);
  249. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  250. goto out;
  251. }
  252. gid = v9fs_get_fsgid_for_create(dir);
  253. mode = omode;
  254. /* Update mode based on ACL value */
  255. err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
  256. if (err) {
  257. p9_debug(P9_DEBUG_VFS, "Failed to get acl values in creat %d\n",
  258. err);
  259. goto error;
  260. }
  261. err = p9_client_create_dotl(ofid, name, v9fs_open_to_dotl_flags(flags),
  262. mode, gid, &qid);
  263. if (err < 0) {
  264. p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in creat %d\n",
  265. err);
  266. goto error;
  267. }
  268. v9fs_invalidate_inode_attr(dir);
  269. /* instantiate inode and assign the unopened fid to the dentry */
  270. fid = p9_client_walk(dfid, 1, &name, 1);
  271. if (IS_ERR(fid)) {
  272. err = PTR_ERR(fid);
  273. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  274. fid = NULL;
  275. goto error;
  276. }
  277. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  278. if (IS_ERR(inode)) {
  279. err = PTR_ERR(inode);
  280. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err);
  281. goto error;
  282. }
  283. /* Now set the ACL based on the default value */
  284. v9fs_set_create_acl(inode, fid, dacl, pacl);
  285. v9fs_fid_add(dentry, fid);
  286. d_instantiate(dentry, inode);
  287. v9inode = V9FS_I(inode);
  288. mutex_lock(&v9inode->v_mutex);
  289. if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
  290. !v9inode->writeback_fid &&
  291. ((flags & O_ACCMODE) != O_RDONLY)) {
  292. /*
  293. * clone a fid and add it to writeback_fid
  294. * we do it during open time instead of
  295. * page dirty time via write_begin/page_mkwrite
  296. * because we want write after unlink usecase
  297. * to work.
  298. */
  299. inode_fid = v9fs_writeback_fid(dentry);
  300. if (IS_ERR(inode_fid)) {
  301. err = PTR_ERR(inode_fid);
  302. mutex_unlock(&v9inode->v_mutex);
  303. goto err_clunk_old_fid;
  304. }
  305. v9inode->writeback_fid = (void *) inode_fid;
  306. }
  307. mutex_unlock(&v9inode->v_mutex);
  308. /* Since we are opening a file, assign the open fid to the file */
  309. err = finish_open(file, dentry, generic_file_open, opened);
  310. if (err)
  311. goto err_clunk_old_fid;
  312. file->private_data = ofid;
  313. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  314. v9fs_cache_inode_set_cookie(inode, file);
  315. *opened |= FILE_CREATED;
  316. out:
  317. v9fs_put_acl(dacl, pacl);
  318. dput(res);
  319. return err;
  320. error:
  321. if (fid)
  322. p9_client_clunk(fid);
  323. err_clunk_old_fid:
  324. if (ofid)
  325. p9_client_clunk(ofid);
  326. goto out;
  327. }
  328. /**
  329. * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
  330. * @dir: inode that is being unlinked
  331. * @dentry: dentry that is being unlinked
  332. * @omode: mode for new directory
  333. *
  334. */
  335. static int v9fs_vfs_mkdir_dotl(struct inode *dir,
  336. struct dentry *dentry, umode_t omode)
  337. {
  338. int err;
  339. struct v9fs_session_info *v9ses;
  340. struct p9_fid *fid = NULL, *dfid = NULL;
  341. kgid_t gid;
  342. char *name;
  343. umode_t mode;
  344. struct inode *inode;
  345. struct p9_qid qid;
  346. struct posix_acl *dacl = NULL, *pacl = NULL;
  347. p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
  348. err = 0;
  349. v9ses = v9fs_inode2v9ses(dir);
  350. omode |= S_IFDIR;
  351. if (dir->i_mode & S_ISGID)
  352. omode |= S_ISGID;
  353. dfid = v9fs_parent_fid(dentry);
  354. if (IS_ERR(dfid)) {
  355. err = PTR_ERR(dfid);
  356. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  357. dfid = NULL;
  358. goto error;
  359. }
  360. gid = v9fs_get_fsgid_for_create(dir);
  361. mode = omode;
  362. /* Update mode based on ACL value */
  363. err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
  364. if (err) {
  365. p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n",
  366. err);
  367. goto error;
  368. }
  369. name = (char *) dentry->d_name.name;
  370. err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
  371. if (err < 0)
  372. goto error;
  373. fid = p9_client_walk(dfid, 1, &name, 1);
  374. if (IS_ERR(fid)) {
  375. err = PTR_ERR(fid);
  376. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  377. err);
  378. fid = NULL;
  379. goto error;
  380. }
  381. /* instantiate inode and assign the unopened fid to the dentry */
  382. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  383. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  384. if (IS_ERR(inode)) {
  385. err = PTR_ERR(inode);
  386. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
  387. err);
  388. goto error;
  389. }
  390. v9fs_fid_add(dentry, fid);
  391. v9fs_set_create_acl(inode, fid, dacl, pacl);
  392. d_instantiate(dentry, inode);
  393. fid = NULL;
  394. err = 0;
  395. } else {
  396. /*
  397. * Not in cached mode. No need to populate
  398. * inode with stat. We need to get an inode
  399. * so that we can set the acl with dentry
  400. */
  401. inode = v9fs_get_inode(dir->i_sb, mode, 0);
  402. if (IS_ERR(inode)) {
  403. err = PTR_ERR(inode);
  404. goto error;
  405. }
  406. v9fs_set_create_acl(inode, fid, dacl, pacl);
  407. d_instantiate(dentry, inode);
  408. }
  409. inc_nlink(dir);
  410. v9fs_invalidate_inode_attr(dir);
  411. error:
  412. if (fid)
  413. p9_client_clunk(fid);
  414. v9fs_put_acl(dacl, pacl);
  415. return err;
  416. }
  417. static int
  418. v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
  419. struct kstat *stat)
  420. {
  421. struct v9fs_session_info *v9ses;
  422. struct p9_fid *fid;
  423. struct p9_stat_dotl *st;
  424. p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  425. v9ses = v9fs_dentry2v9ses(dentry);
  426. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  427. generic_fillattr(d_inode(dentry), stat);
  428. return 0;
  429. }
  430. fid = v9fs_fid_lookup(dentry);
  431. if (IS_ERR(fid))
  432. return PTR_ERR(fid);
  433. /* Ask for all the fields in stat structure. Server will return
  434. * whatever it supports
  435. */
  436. st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
  437. if (IS_ERR(st))
  438. return PTR_ERR(st);
  439. v9fs_stat2inode_dotl(st, d_inode(dentry));
  440. generic_fillattr(d_inode(dentry), stat);
  441. /* Change block size to what the server returned */
  442. stat->blksize = st->st_blksize;
  443. kfree(st);
  444. return 0;
  445. }
  446. /*
  447. * Attribute flags.
  448. */
  449. #define P9_ATTR_MODE (1 << 0)
  450. #define P9_ATTR_UID (1 << 1)
  451. #define P9_ATTR_GID (1 << 2)
  452. #define P9_ATTR_SIZE (1 << 3)
  453. #define P9_ATTR_ATIME (1 << 4)
  454. #define P9_ATTR_MTIME (1 << 5)
  455. #define P9_ATTR_CTIME (1 << 6)
  456. #define P9_ATTR_ATIME_SET (1 << 7)
  457. #define P9_ATTR_MTIME_SET (1 << 8)
  458. struct dotl_iattr_map {
  459. int iattr_valid;
  460. int p9_iattr_valid;
  461. };
  462. static int v9fs_mapped_iattr_valid(int iattr_valid)
  463. {
  464. int i;
  465. int p9_iattr_valid = 0;
  466. struct dotl_iattr_map dotl_iattr_map[] = {
  467. { ATTR_MODE, P9_ATTR_MODE },
  468. { ATTR_UID, P9_ATTR_UID },
  469. { ATTR_GID, P9_ATTR_GID },
  470. { ATTR_SIZE, P9_ATTR_SIZE },
  471. { ATTR_ATIME, P9_ATTR_ATIME },
  472. { ATTR_MTIME, P9_ATTR_MTIME },
  473. { ATTR_CTIME, P9_ATTR_CTIME },
  474. { ATTR_ATIME_SET, P9_ATTR_ATIME_SET },
  475. { ATTR_MTIME_SET, P9_ATTR_MTIME_SET },
  476. };
  477. for (i = 0; i < ARRAY_SIZE(dotl_iattr_map); i++) {
  478. if (iattr_valid & dotl_iattr_map[i].iattr_valid)
  479. p9_iattr_valid |= dotl_iattr_map[i].p9_iattr_valid;
  480. }
  481. return p9_iattr_valid;
  482. }
  483. /**
  484. * v9fs_vfs_setattr_dotl - set file metadata
  485. * @dentry: file whose metadata to set
  486. * @iattr: metadata assignment structure
  487. *
  488. */
  489. int v9fs_vfs_setattr_dotl(struct dentry *dentry, struct iattr *iattr)
  490. {
  491. int retval;
  492. struct p9_fid *fid;
  493. struct p9_iattr_dotl p9attr;
  494. struct inode *inode = d_inode(dentry);
  495. p9_debug(P9_DEBUG_VFS, "\n");
  496. retval = setattr_prepare(dentry, iattr);
  497. if (retval)
  498. return retval;
  499. p9attr.valid = v9fs_mapped_iattr_valid(iattr->ia_valid);
  500. p9attr.mode = iattr->ia_mode;
  501. p9attr.uid = iattr->ia_uid;
  502. p9attr.gid = iattr->ia_gid;
  503. p9attr.size = iattr->ia_size;
  504. p9attr.atime_sec = iattr->ia_atime.tv_sec;
  505. p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
  506. p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
  507. p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
  508. fid = v9fs_fid_lookup(dentry);
  509. if (IS_ERR(fid))
  510. return PTR_ERR(fid);
  511. /* Write all dirty data */
  512. if (S_ISREG(inode->i_mode))
  513. filemap_write_and_wait(inode->i_mapping);
  514. retval = p9_client_setattr(fid, &p9attr);
  515. if (retval < 0)
  516. return retval;
  517. if ((iattr->ia_valid & ATTR_SIZE) &&
  518. iattr->ia_size != i_size_read(inode))
  519. truncate_setsize(inode, iattr->ia_size);
  520. v9fs_invalidate_inode_attr(inode);
  521. setattr_copy(inode, iattr);
  522. mark_inode_dirty(inode);
  523. if (iattr->ia_valid & ATTR_MODE) {
  524. /* We also want to update ACL when we update mode bits */
  525. retval = v9fs_acl_chmod(inode, fid);
  526. if (retval < 0)
  527. return retval;
  528. }
  529. return 0;
  530. }
  531. /**
  532. * v9fs_stat2inode_dotl - populate an inode structure with stat info
  533. * @stat: stat structure
  534. * @inode: inode to populate
  535. *
  536. */
  537. void
  538. v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
  539. {
  540. umode_t mode;
  541. struct v9fs_inode *v9inode = V9FS_I(inode);
  542. if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
  543. inode->i_atime.tv_sec = stat->st_atime_sec;
  544. inode->i_atime.tv_nsec = stat->st_atime_nsec;
  545. inode->i_mtime.tv_sec = stat->st_mtime_sec;
  546. inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
  547. inode->i_ctime.tv_sec = stat->st_ctime_sec;
  548. inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
  549. inode->i_uid = stat->st_uid;
  550. inode->i_gid = stat->st_gid;
  551. set_nlink(inode, stat->st_nlink);
  552. mode = stat->st_mode & S_IALLUGO;
  553. mode |= inode->i_mode & ~S_IALLUGO;
  554. inode->i_mode = mode;
  555. i_size_write(inode, stat->st_size);
  556. inode->i_blocks = stat->st_blocks;
  557. } else {
  558. if (stat->st_result_mask & P9_STATS_ATIME) {
  559. inode->i_atime.tv_sec = stat->st_atime_sec;
  560. inode->i_atime.tv_nsec = stat->st_atime_nsec;
  561. }
  562. if (stat->st_result_mask & P9_STATS_MTIME) {
  563. inode->i_mtime.tv_sec = stat->st_mtime_sec;
  564. inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
  565. }
  566. if (stat->st_result_mask & P9_STATS_CTIME) {
  567. inode->i_ctime.tv_sec = stat->st_ctime_sec;
  568. inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
  569. }
  570. if (stat->st_result_mask & P9_STATS_UID)
  571. inode->i_uid = stat->st_uid;
  572. if (stat->st_result_mask & P9_STATS_GID)
  573. inode->i_gid = stat->st_gid;
  574. if (stat->st_result_mask & P9_STATS_NLINK)
  575. set_nlink(inode, stat->st_nlink);
  576. if (stat->st_result_mask & P9_STATS_MODE) {
  577. inode->i_mode = stat->st_mode;
  578. if ((S_ISBLK(inode->i_mode)) ||
  579. (S_ISCHR(inode->i_mode)))
  580. init_special_inode(inode, inode->i_mode,
  581. inode->i_rdev);
  582. }
  583. if (stat->st_result_mask & P9_STATS_RDEV)
  584. inode->i_rdev = new_decode_dev(stat->st_rdev);
  585. if (stat->st_result_mask & P9_STATS_SIZE)
  586. i_size_write(inode, stat->st_size);
  587. if (stat->st_result_mask & P9_STATS_BLOCKS)
  588. inode->i_blocks = stat->st_blocks;
  589. }
  590. if (stat->st_result_mask & P9_STATS_GEN)
  591. inode->i_generation = stat->st_gen;
  592. /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
  593. * because the inode structure does not have fields for them.
  594. */
  595. v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
  596. }
  597. static int
  598. v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
  599. const char *symname)
  600. {
  601. int err;
  602. kgid_t gid;
  603. char *name;
  604. struct p9_qid qid;
  605. struct inode *inode;
  606. struct p9_fid *dfid;
  607. struct p9_fid *fid = NULL;
  608. struct v9fs_session_info *v9ses;
  609. name = (char *) dentry->d_name.name;
  610. p9_debug(P9_DEBUG_VFS, "%lu,%s,%s\n", dir->i_ino, name, symname);
  611. v9ses = v9fs_inode2v9ses(dir);
  612. dfid = v9fs_parent_fid(dentry);
  613. if (IS_ERR(dfid)) {
  614. err = PTR_ERR(dfid);
  615. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  616. return err;
  617. }
  618. gid = v9fs_get_fsgid_for_create(dir);
  619. /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
  620. err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
  621. if (err < 0) {
  622. p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
  623. goto error;
  624. }
  625. v9fs_invalidate_inode_attr(dir);
  626. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  627. /* Now walk from the parent so we can get an unopened fid. */
  628. fid = p9_client_walk(dfid, 1, &name, 1);
  629. if (IS_ERR(fid)) {
  630. err = PTR_ERR(fid);
  631. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  632. err);
  633. fid = NULL;
  634. goto error;
  635. }
  636. /* instantiate inode and assign the unopened fid to dentry */
  637. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  638. if (IS_ERR(inode)) {
  639. err = PTR_ERR(inode);
  640. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
  641. err);
  642. goto error;
  643. }
  644. v9fs_fid_add(dentry, fid);
  645. d_instantiate(dentry, inode);
  646. fid = NULL;
  647. err = 0;
  648. } else {
  649. /* Not in cached mode. No need to populate inode with stat */
  650. inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
  651. if (IS_ERR(inode)) {
  652. err = PTR_ERR(inode);
  653. goto error;
  654. }
  655. d_instantiate(dentry, inode);
  656. }
  657. error:
  658. if (fid)
  659. p9_client_clunk(fid);
  660. return err;
  661. }
  662. /**
  663. * v9fs_vfs_link_dotl - create a hardlink for dotl
  664. * @old_dentry: dentry for file to link to
  665. * @dir: inode destination for new link
  666. * @dentry: dentry for link
  667. *
  668. */
  669. static int
  670. v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
  671. struct dentry *dentry)
  672. {
  673. int err;
  674. struct p9_fid *dfid, *oldfid;
  675. struct v9fs_session_info *v9ses;
  676. p9_debug(P9_DEBUG_VFS, "dir ino: %lu, old_name: %pd, new_name: %pd\n",
  677. dir->i_ino, old_dentry, dentry);
  678. v9ses = v9fs_inode2v9ses(dir);
  679. dfid = v9fs_parent_fid(dentry);
  680. if (IS_ERR(dfid))
  681. return PTR_ERR(dfid);
  682. oldfid = v9fs_fid_lookup(old_dentry);
  683. if (IS_ERR(oldfid))
  684. return PTR_ERR(oldfid);
  685. err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
  686. if (err < 0) {
  687. p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
  688. return err;
  689. }
  690. v9fs_invalidate_inode_attr(dir);
  691. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  692. /* Get the latest stat info from server. */
  693. struct p9_fid *fid;
  694. fid = v9fs_fid_lookup(old_dentry);
  695. if (IS_ERR(fid))
  696. return PTR_ERR(fid);
  697. v9fs_refresh_inode_dotl(fid, d_inode(old_dentry));
  698. }
  699. ihold(d_inode(old_dentry));
  700. d_instantiate(dentry, d_inode(old_dentry));
  701. return err;
  702. }
  703. /**
  704. * v9fs_vfs_mknod_dotl - create a special file
  705. * @dir: inode destination for new link
  706. * @dentry: dentry for file
  707. * @omode: mode for creation
  708. * @rdev: device associated with special file
  709. *
  710. */
  711. static int
  712. v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
  713. dev_t rdev)
  714. {
  715. int err;
  716. kgid_t gid;
  717. char *name;
  718. umode_t mode;
  719. struct v9fs_session_info *v9ses;
  720. struct p9_fid *fid = NULL, *dfid = NULL;
  721. struct inode *inode;
  722. struct p9_qid qid;
  723. struct posix_acl *dacl = NULL, *pacl = NULL;
  724. p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %hx MAJOR: %u MINOR: %u\n",
  725. dir->i_ino, dentry, omode,
  726. MAJOR(rdev), MINOR(rdev));
  727. v9ses = v9fs_inode2v9ses(dir);
  728. dfid = v9fs_parent_fid(dentry);
  729. if (IS_ERR(dfid)) {
  730. err = PTR_ERR(dfid);
  731. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  732. dfid = NULL;
  733. goto error;
  734. }
  735. gid = v9fs_get_fsgid_for_create(dir);
  736. mode = omode;
  737. /* Update mode based on ACL value */
  738. err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
  739. if (err) {
  740. p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n",
  741. err);
  742. goto error;
  743. }
  744. name = (char *) dentry->d_name.name;
  745. err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
  746. if (err < 0)
  747. goto error;
  748. v9fs_invalidate_inode_attr(dir);
  749. fid = p9_client_walk(dfid, 1, &name, 1);
  750. if (IS_ERR(fid)) {
  751. err = PTR_ERR(fid);
  752. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  753. err);
  754. fid = NULL;
  755. goto error;
  756. }
  757. /* instantiate inode and assign the unopened fid to the dentry */
  758. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  759. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  760. if (IS_ERR(inode)) {
  761. err = PTR_ERR(inode);
  762. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
  763. err);
  764. goto error;
  765. }
  766. v9fs_set_create_acl(inode, fid, dacl, pacl);
  767. v9fs_fid_add(dentry, fid);
  768. d_instantiate(dentry, inode);
  769. fid = NULL;
  770. err = 0;
  771. } else {
  772. /*
  773. * Not in cached mode. No need to populate inode with stat.
  774. * socket syscall returns a fd, so we need instantiate
  775. */
  776. inode = v9fs_get_inode(dir->i_sb, mode, rdev);
  777. if (IS_ERR(inode)) {
  778. err = PTR_ERR(inode);
  779. goto error;
  780. }
  781. v9fs_set_create_acl(inode, fid, dacl, pacl);
  782. d_instantiate(dentry, inode);
  783. }
  784. error:
  785. if (fid)
  786. p9_client_clunk(fid);
  787. v9fs_put_acl(dacl, pacl);
  788. return err;
  789. }
  790. /**
  791. * v9fs_vfs_get_link_dotl - follow a symlink path
  792. * @dentry: dentry for symlink
  793. * @inode: inode for symlink
  794. * @done: destructor for return value
  795. */
  796. static const char *
  797. v9fs_vfs_get_link_dotl(struct dentry *dentry,
  798. struct inode *inode,
  799. struct delayed_call *done)
  800. {
  801. struct p9_fid *fid;
  802. char *target;
  803. int retval;
  804. if (!dentry)
  805. return ERR_PTR(-ECHILD);
  806. p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
  807. fid = v9fs_fid_lookup(dentry);
  808. if (IS_ERR(fid))
  809. return ERR_CAST(fid);
  810. retval = p9_client_readlink(fid, &target);
  811. if (retval)
  812. return ERR_PTR(retval);
  813. set_delayed_call(done, kfree_link, target);
  814. return target;
  815. }
  816. int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
  817. {
  818. loff_t i_size;
  819. struct p9_stat_dotl *st;
  820. struct v9fs_session_info *v9ses;
  821. v9ses = v9fs_inode2v9ses(inode);
  822. st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
  823. if (IS_ERR(st))
  824. return PTR_ERR(st);
  825. /*
  826. * Don't update inode if the file type is different
  827. */
  828. if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
  829. goto out;
  830. spin_lock(&inode->i_lock);
  831. /*
  832. * We don't want to refresh inode->i_size,
  833. * because we may have cached data
  834. */
  835. i_size = inode->i_size;
  836. v9fs_stat2inode_dotl(st, inode);
  837. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  838. inode->i_size = i_size;
  839. spin_unlock(&inode->i_lock);
  840. out:
  841. kfree(st);
  842. return 0;
  843. }
  844. const struct inode_operations v9fs_dir_inode_operations_dotl = {
  845. .create = v9fs_vfs_create_dotl,
  846. .atomic_open = v9fs_vfs_atomic_open_dotl,
  847. .lookup = v9fs_vfs_lookup,
  848. .link = v9fs_vfs_link_dotl,
  849. .symlink = v9fs_vfs_symlink_dotl,
  850. .unlink = v9fs_vfs_unlink,
  851. .mkdir = v9fs_vfs_mkdir_dotl,
  852. .rmdir = v9fs_vfs_rmdir,
  853. .mknod = v9fs_vfs_mknod_dotl,
  854. .rename = v9fs_vfs_rename,
  855. .getattr = v9fs_vfs_getattr_dotl,
  856. .setattr = v9fs_vfs_setattr_dotl,
  857. .listxattr = v9fs_listxattr,
  858. .get_acl = v9fs_iop_get_acl,
  859. };
  860. const struct inode_operations v9fs_file_inode_operations_dotl = {
  861. .getattr = v9fs_vfs_getattr_dotl,
  862. .setattr = v9fs_vfs_setattr_dotl,
  863. .listxattr = v9fs_listxattr,
  864. .get_acl = v9fs_iop_get_acl,
  865. };
  866. const struct inode_operations v9fs_symlink_inode_operations_dotl = {
  867. .readlink = generic_readlink,
  868. .get_link = v9fs_vfs_get_link_dotl,
  869. .getattr = v9fs_vfs_getattr_dotl,
  870. .setattr = v9fs_vfs_setattr_dotl,
  871. .listxattr = v9fs_listxattr,
  872. };