vfs_inode_dotl.c 23 KB

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