vfs_inode_dotl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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_state_read_once(inode) & 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. if (d_in_lookup(dentry)) {
  211. struct dentry *res = v9fs_vfs_lookup(dir, dentry, 0);
  212. if (res || d_really_is_positive(dentry))
  213. return finish_no_open(file, res);
  214. }
  215. /* Only creates */
  216. if (!(flags & O_CREAT))
  217. return finish_no_open(file, NULL);
  218. v9ses = v9fs_inode2v9ses(dir);
  219. name = dentry->d_name.name;
  220. p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%x\n",
  221. name, flags, omode);
  222. dfid = v9fs_parent_fid(dentry);
  223. if (IS_ERR(dfid)) {
  224. err = PTR_ERR(dfid);
  225. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  226. goto out;
  227. }
  228. /* clone a fid to use for creation */
  229. ofid = clone_fid(dfid);
  230. if (IS_ERR(ofid)) {
  231. err = PTR_ERR(ofid);
  232. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  233. goto out;
  234. }
  235. gid = v9fs_get_fsgid_for_create(dir);
  236. mode = omode;
  237. /* Update mode based on ACL value */
  238. err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
  239. if (err) {
  240. p9_debug(P9_DEBUG_VFS, "Failed to get acl values in create %d\n",
  241. err);
  242. goto out;
  243. }
  244. if ((v9ses->cache & CACHE_WRITEBACK) && (p9_omode & P9_OWRITE)) {
  245. p9_omode = (p9_omode & ~(P9_OWRITE | P9_DOTL_APPEND)) | P9_ORDWR;
  246. p9_debug(P9_DEBUG_CACHE,
  247. "write-only file with writeback enabled, creating w/ O_RDWR\n");
  248. }
  249. err = p9_client_create_dotl(ofid, name, p9_omode, mode, gid, &qid);
  250. if (err < 0) {
  251. p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in create %d\n",
  252. err);
  253. goto out;
  254. }
  255. v9fs_invalidate_inode_attr(dir);
  256. /* instantiate inode and assign the unopened fid to the dentry */
  257. fid = p9_client_walk(dfid, 1, &name, 1);
  258. if (IS_ERR(fid)) {
  259. err = PTR_ERR(fid);
  260. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  261. goto out;
  262. }
  263. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  264. if (IS_ERR(inode)) {
  265. err = PTR_ERR(inode);
  266. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err);
  267. goto out;
  268. }
  269. /* Now set the ACL based on the default value */
  270. v9fs_set_create_acl(inode, fid, dacl, pacl);
  271. v9fs_fid_add(dentry, &fid);
  272. d_instantiate(dentry, inode);
  273. /* Since we are opening a file, assign the open fid to the file */
  274. err = finish_open(file, dentry, generic_file_open);
  275. if (err)
  276. goto out;
  277. file->private_data = ofid;
  278. #ifdef CONFIG_9P_FSCACHE
  279. if (v9ses->cache & CACHE_FSCACHE) {
  280. struct v9fs_inode *v9inode = V9FS_I(inode);
  281. fscache_use_cookie(v9fs_inode_cookie(v9inode),
  282. file->f_mode & FMODE_WRITE);
  283. }
  284. #endif
  285. v9fs_fid_add_modes(ofid, v9ses->flags, v9ses->cache, flags);
  286. v9fs_open_fid_add(inode, &ofid);
  287. file->f_mode |= FMODE_CREATED;
  288. out:
  289. p9_fid_put(dfid);
  290. p9_fid_put(ofid);
  291. p9_fid_put(fid);
  292. v9fs_put_acl(dacl, pacl);
  293. return err;
  294. }
  295. /**
  296. * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
  297. * @idmap: The idmap of the mount
  298. * @dir: inode that is being unlinked
  299. * @dentry: dentry that is being unlinked
  300. * @omode: mode for new directory
  301. *
  302. */
  303. static struct dentry *v9fs_vfs_mkdir_dotl(struct mnt_idmap *idmap,
  304. struct inode *dir, struct dentry *dentry,
  305. umode_t omode)
  306. {
  307. int err;
  308. struct v9fs_session_info *v9ses;
  309. struct p9_fid *fid = NULL, *dfid = NULL;
  310. kgid_t gid;
  311. const unsigned char *name;
  312. umode_t mode;
  313. struct inode *inode;
  314. struct p9_qid qid;
  315. struct posix_acl *dacl = NULL, *pacl = NULL;
  316. p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
  317. v9ses = v9fs_inode2v9ses(dir);
  318. omode |= S_IFDIR;
  319. if (dir->i_mode & S_ISGID)
  320. omode |= S_ISGID;
  321. dfid = v9fs_parent_fid(dentry);
  322. if (IS_ERR(dfid)) {
  323. err = PTR_ERR(dfid);
  324. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  325. goto error;
  326. }
  327. gid = v9fs_get_fsgid_for_create(dir);
  328. mode = omode;
  329. /* Update mode based on ACL value */
  330. err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
  331. if (err) {
  332. p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n",
  333. err);
  334. goto error;
  335. }
  336. name = dentry->d_name.name;
  337. err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
  338. if (err < 0)
  339. goto error;
  340. fid = p9_client_walk(dfid, 1, &name, 1);
  341. if (IS_ERR(fid)) {
  342. err = PTR_ERR(fid);
  343. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  344. err);
  345. goto error;
  346. }
  347. /* instantiate inode and assign the unopened fid to the dentry */
  348. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  349. if (IS_ERR(inode)) {
  350. err = PTR_ERR(inode);
  351. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
  352. err);
  353. goto error;
  354. }
  355. v9fs_set_create_acl(inode, fid, dacl, pacl);
  356. v9fs_fid_add(dentry, &fid);
  357. d_instantiate(dentry, inode);
  358. err = 0;
  359. inc_nlink(dir);
  360. v9fs_invalidate_inode_attr(dir);
  361. error:
  362. p9_fid_put(fid);
  363. v9fs_put_acl(dacl, pacl);
  364. p9_fid_put(dfid);
  365. return ERR_PTR(err);
  366. }
  367. static int
  368. v9fs_vfs_getattr_dotl(struct mnt_idmap *idmap,
  369. const struct path *path, struct kstat *stat,
  370. u32 request_mask, unsigned int flags)
  371. {
  372. struct dentry *dentry = path->dentry;
  373. struct v9fs_session_info *v9ses;
  374. struct p9_fid *fid;
  375. struct inode *inode = d_inode(dentry);
  376. struct p9_stat_dotl *st;
  377. p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  378. v9ses = v9fs_dentry2v9ses(dentry);
  379. if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
  380. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  381. return 0;
  382. } else if (v9ses->cache) {
  383. if (S_ISREG(inode->i_mode)) {
  384. int retval = filemap_fdatawrite(inode->i_mapping);
  385. if (retval)
  386. p9_debug(P9_DEBUG_ERROR,
  387. "flushing writeback during getattr returned %d\n", retval);
  388. }
  389. }
  390. fid = v9fs_fid_lookup(dentry);
  391. if (IS_ERR(fid))
  392. return PTR_ERR(fid);
  393. /* Ask for all the fields in stat structure. Server will return
  394. * whatever it supports
  395. */
  396. st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
  397. p9_fid_put(fid);
  398. if (IS_ERR(st))
  399. return PTR_ERR(st);
  400. v9fs_stat2inode_dotl(st, d_inode(dentry), 0);
  401. generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(dentry), stat);
  402. /* Change block size to what the server returned */
  403. stat->blksize = st->st_blksize;
  404. kfree(st);
  405. return 0;
  406. }
  407. /*
  408. * Attribute flags.
  409. */
  410. #define P9_ATTR_MODE (1 << 0)
  411. #define P9_ATTR_UID (1 << 1)
  412. #define P9_ATTR_GID (1 << 2)
  413. #define P9_ATTR_SIZE (1 << 3)
  414. #define P9_ATTR_ATIME (1 << 4)
  415. #define P9_ATTR_MTIME (1 << 5)
  416. #define P9_ATTR_CTIME (1 << 6)
  417. #define P9_ATTR_ATIME_SET (1 << 7)
  418. #define P9_ATTR_MTIME_SET (1 << 8)
  419. struct dotl_iattr_map {
  420. int iattr_valid;
  421. int p9_iattr_valid;
  422. };
  423. static int v9fs_mapped_iattr_valid(int iattr_valid)
  424. {
  425. int i;
  426. int p9_iattr_valid = 0;
  427. struct dotl_iattr_map dotl_iattr_map[] = {
  428. { ATTR_MODE, P9_ATTR_MODE },
  429. { ATTR_UID, P9_ATTR_UID },
  430. { ATTR_GID, P9_ATTR_GID },
  431. { ATTR_SIZE, P9_ATTR_SIZE },
  432. { ATTR_ATIME, P9_ATTR_ATIME },
  433. { ATTR_MTIME, P9_ATTR_MTIME },
  434. { ATTR_CTIME, P9_ATTR_CTIME },
  435. { ATTR_ATIME_SET, P9_ATTR_ATIME_SET },
  436. { ATTR_MTIME_SET, P9_ATTR_MTIME_SET },
  437. };
  438. for (i = 0; i < ARRAY_SIZE(dotl_iattr_map); i++) {
  439. if (iattr_valid & dotl_iattr_map[i].iattr_valid)
  440. p9_iattr_valid |= dotl_iattr_map[i].p9_iattr_valid;
  441. }
  442. return p9_iattr_valid;
  443. }
  444. /**
  445. * v9fs_vfs_setattr_dotl - set file metadata
  446. * @idmap: idmap of the mount
  447. * @dentry: file whose metadata to set
  448. * @iattr: metadata assignment structure
  449. *
  450. */
  451. int v9fs_vfs_setattr_dotl(struct mnt_idmap *idmap,
  452. struct dentry *dentry, struct iattr *iattr)
  453. {
  454. int retval, use_dentry = 0;
  455. struct inode *inode = d_inode(dentry);
  456. struct v9fs_session_info __maybe_unused *v9ses;
  457. struct p9_fid *fid = NULL;
  458. struct p9_iattr_dotl p9attr = {
  459. .uid = INVALID_UID,
  460. .gid = INVALID_GID,
  461. };
  462. p9_debug(P9_DEBUG_VFS, "\n");
  463. retval = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
  464. if (retval)
  465. return retval;
  466. v9ses = v9fs_dentry2v9ses(dentry);
  467. p9attr.valid = v9fs_mapped_iattr_valid(iattr->ia_valid);
  468. if (iattr->ia_valid & ATTR_MODE)
  469. p9attr.mode = iattr->ia_mode;
  470. if (iattr->ia_valid & ATTR_UID)
  471. p9attr.uid = iattr->ia_uid;
  472. if (iattr->ia_valid & ATTR_GID)
  473. p9attr.gid = iattr->ia_gid;
  474. if (iattr->ia_valid & ATTR_SIZE)
  475. p9attr.size = iattr->ia_size;
  476. if (iattr->ia_valid & ATTR_ATIME_SET) {
  477. p9attr.atime_sec = iattr->ia_atime.tv_sec;
  478. p9attr.atime_nsec = iattr->ia_atime.tv_nsec;
  479. }
  480. if (iattr->ia_valid & ATTR_MTIME_SET) {
  481. p9attr.mtime_sec = iattr->ia_mtime.tv_sec;
  482. p9attr.mtime_nsec = iattr->ia_mtime.tv_nsec;
  483. }
  484. if (iattr->ia_valid & ATTR_FILE) {
  485. fid = iattr->ia_file->private_data;
  486. WARN_ON(!fid);
  487. }
  488. if (!fid) {
  489. fid = v9fs_fid_lookup(dentry);
  490. use_dentry = 1;
  491. }
  492. if (IS_ERR(fid))
  493. return PTR_ERR(fid);
  494. /* Write all dirty data */
  495. if (S_ISREG(inode->i_mode)) {
  496. retval = filemap_fdatawrite(inode->i_mapping);
  497. if (retval < 0)
  498. p9_debug(P9_DEBUG_ERROR,
  499. "Flushing file prior to setattr failed: %d\n", retval);
  500. }
  501. retval = p9_client_setattr(fid, &p9attr);
  502. if (retval < 0) {
  503. if (use_dentry)
  504. p9_fid_put(fid);
  505. return retval;
  506. }
  507. if ((iattr->ia_valid & ATTR_SIZE) && iattr->ia_size !=
  508. i_size_read(inode)) {
  509. truncate_setsize(inode, iattr->ia_size);
  510. netfs_resize_file(netfs_inode(inode), iattr->ia_size, true);
  511. #ifdef CONFIG_9P_FSCACHE
  512. if (v9ses->cache & CACHE_FSCACHE)
  513. fscache_resize_cookie(v9fs_inode_cookie(V9FS_I(inode)),
  514. iattr->ia_size);
  515. #endif
  516. }
  517. v9fs_invalidate_inode_attr(inode);
  518. setattr_copy(&nop_mnt_idmap, inode, iattr);
  519. mark_inode_dirty(inode);
  520. if (iattr->ia_valid & ATTR_MODE) {
  521. /* We also want to update ACL when we update mode bits */
  522. retval = v9fs_acl_chmod(inode, fid);
  523. if (retval < 0) {
  524. if (use_dentry)
  525. p9_fid_put(fid);
  526. return retval;
  527. }
  528. }
  529. if (use_dentry)
  530. p9_fid_put(fid);
  531. return 0;
  532. }
  533. /**
  534. * v9fs_stat2inode_dotl - populate an inode structure with stat info
  535. * @stat: stat structure
  536. * @inode: inode to populate
  537. * @flags: ctrl flags (e.g. V9FS_STAT2INODE_KEEP_ISIZE)
  538. *
  539. */
  540. void
  541. v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode,
  542. unsigned int flags)
  543. {
  544. umode_t mode;
  545. struct v9fs_inode *v9inode = V9FS_I(inode);
  546. if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
  547. inode_set_atime(inode, stat->st_atime_sec,
  548. stat->st_atime_nsec);
  549. inode_set_mtime(inode, stat->st_mtime_sec,
  550. stat->st_mtime_nsec);
  551. inode_set_ctime(inode, stat->st_ctime_sec,
  552. stat->st_ctime_nsec);
  553. inode->i_uid = stat->st_uid;
  554. inode->i_gid = stat->st_gid;
  555. set_nlink(inode, stat->st_nlink);
  556. mode = stat->st_mode & S_IALLUGO;
  557. mode |= inode->i_mode & ~S_IALLUGO;
  558. inode->i_mode = mode;
  559. v9inode->netfs.remote_i_size = stat->st_size;
  560. if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE))
  561. v9fs_i_size_write(inode, stat->st_size);
  562. inode->i_blocks = stat->st_blocks;
  563. } else {
  564. if (stat->st_result_mask & P9_STATS_ATIME) {
  565. inode_set_atime(inode, stat->st_atime_sec,
  566. stat->st_atime_nsec);
  567. }
  568. if (stat->st_result_mask & P9_STATS_MTIME) {
  569. inode_set_mtime(inode, stat->st_mtime_sec,
  570. stat->st_mtime_nsec);
  571. }
  572. if (stat->st_result_mask & P9_STATS_CTIME) {
  573. inode_set_ctime(inode, stat->st_ctime_sec,
  574. stat->st_ctime_nsec);
  575. }
  576. if (stat->st_result_mask & P9_STATS_UID)
  577. inode->i_uid = stat->st_uid;
  578. if (stat->st_result_mask & P9_STATS_GID)
  579. inode->i_gid = stat->st_gid;
  580. if (stat->st_result_mask & P9_STATS_NLINK)
  581. set_nlink(inode, stat->st_nlink);
  582. if (stat->st_result_mask & P9_STATS_MODE) {
  583. mode = stat->st_mode & S_IALLUGO;
  584. mode |= inode->i_mode & ~S_IALLUGO;
  585. inode->i_mode = mode;
  586. }
  587. if (!(flags & V9FS_STAT2INODE_KEEP_ISIZE) &&
  588. stat->st_result_mask & P9_STATS_SIZE) {
  589. v9inode->netfs.remote_i_size = stat->st_size;
  590. v9fs_i_size_write(inode, stat->st_size);
  591. }
  592. if (stat->st_result_mask & P9_STATS_BLOCKS)
  593. inode->i_blocks = stat->st_blocks;
  594. }
  595. if (stat->st_result_mask & P9_STATS_GEN)
  596. inode->i_generation = stat->st_gen;
  597. /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
  598. * because the inode structure does not have fields for them.
  599. */
  600. v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
  601. }
  602. static int
  603. v9fs_vfs_symlink_dotl(struct mnt_idmap *idmap, struct inode *dir,
  604. struct dentry *dentry, const char *symname)
  605. {
  606. int err;
  607. kgid_t gid;
  608. const unsigned char *name;
  609. struct p9_qid qid;
  610. struct p9_fid *dfid;
  611. struct p9_fid *fid = NULL;
  612. name = dentry->d_name.name;
  613. p9_debug(P9_DEBUG_VFS, "%lu,%s,%s\n", dir->i_ino, name, symname);
  614. dfid = v9fs_parent_fid(dentry);
  615. if (IS_ERR(dfid)) {
  616. err = PTR_ERR(dfid);
  617. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  618. return err;
  619. }
  620. gid = v9fs_get_fsgid_for_create(dir);
  621. /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
  622. err = p9_client_symlink(dfid, name, symname, gid, &qid);
  623. if (err < 0) {
  624. p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
  625. goto error;
  626. }
  627. v9fs_invalidate_inode_attr(dir);
  628. error:
  629. p9_fid_put(fid);
  630. p9_fid_put(dfid);
  631. return err;
  632. }
  633. /**
  634. * v9fs_vfs_link_dotl - create a hardlink for dotl
  635. * @old_dentry: dentry for file to link to
  636. * @dir: inode destination for new link
  637. * @dentry: dentry for link
  638. *
  639. */
  640. static int
  641. v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
  642. struct dentry *dentry)
  643. {
  644. int err;
  645. struct p9_fid *dfid, *oldfid;
  646. struct v9fs_session_info *v9ses;
  647. p9_debug(P9_DEBUG_VFS, "dir ino: %lu, old_name: %pd, new_name: %pd\n",
  648. dir->i_ino, old_dentry, dentry);
  649. v9ses = v9fs_inode2v9ses(dir);
  650. dfid = v9fs_parent_fid(dentry);
  651. if (IS_ERR(dfid))
  652. return PTR_ERR(dfid);
  653. oldfid = v9fs_fid_lookup(old_dentry);
  654. if (IS_ERR(oldfid)) {
  655. p9_fid_put(dfid);
  656. return PTR_ERR(oldfid);
  657. }
  658. err = p9_client_link(dfid, oldfid, dentry->d_name.name);
  659. p9_fid_put(dfid);
  660. p9_fid_put(oldfid);
  661. if (err < 0) {
  662. p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
  663. return err;
  664. }
  665. v9fs_invalidate_inode_attr(dir);
  666. if (v9ses->cache & (CACHE_META|CACHE_LOOSE)) {
  667. /* Get the latest stat info from server. */
  668. struct p9_fid *fid;
  669. fid = v9fs_fid_lookup(old_dentry);
  670. if (IS_ERR(fid))
  671. return PTR_ERR(fid);
  672. v9fs_refresh_inode_dotl(fid, d_inode(old_dentry));
  673. p9_fid_put(fid);
  674. }
  675. ihold(d_inode(old_dentry));
  676. d_instantiate(dentry, d_inode(old_dentry));
  677. return err;
  678. }
  679. /**
  680. * v9fs_vfs_mknod_dotl - create a special file
  681. * @idmap: The idmap of the mount
  682. * @dir: inode destination for new link
  683. * @dentry: dentry for file
  684. * @omode: mode for creation
  685. * @rdev: device associated with special file
  686. *
  687. */
  688. static int
  689. v9fs_vfs_mknod_dotl(struct mnt_idmap *idmap, struct inode *dir,
  690. struct dentry *dentry, umode_t omode, dev_t rdev)
  691. {
  692. int err;
  693. kgid_t gid;
  694. const unsigned char *name;
  695. umode_t mode;
  696. struct v9fs_session_info *v9ses;
  697. struct p9_fid *fid = NULL, *dfid = NULL;
  698. struct inode *inode;
  699. struct p9_qid qid;
  700. struct posix_acl *dacl = NULL, *pacl = NULL;
  701. p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %x MAJOR: %u MINOR: %u\n",
  702. dir->i_ino, dentry, omode,
  703. MAJOR(rdev), MINOR(rdev));
  704. v9ses = v9fs_inode2v9ses(dir);
  705. dfid = v9fs_parent_fid(dentry);
  706. if (IS_ERR(dfid)) {
  707. err = PTR_ERR(dfid);
  708. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  709. goto error;
  710. }
  711. gid = v9fs_get_fsgid_for_create(dir);
  712. mode = omode;
  713. /* Update mode based on ACL value */
  714. err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
  715. if (err) {
  716. p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n",
  717. err);
  718. goto error;
  719. }
  720. name = dentry->d_name.name;
  721. err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
  722. if (err < 0)
  723. goto error;
  724. v9fs_invalidate_inode_attr(dir);
  725. fid = p9_client_walk(dfid, 1, &name, 1);
  726. if (IS_ERR(fid)) {
  727. err = PTR_ERR(fid);
  728. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  729. err);
  730. goto error;
  731. }
  732. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  733. if (IS_ERR(inode)) {
  734. err = PTR_ERR(inode);
  735. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
  736. err);
  737. goto error;
  738. }
  739. v9fs_set_create_acl(inode, fid, dacl, pacl);
  740. v9fs_fid_add(dentry, &fid);
  741. d_instantiate(dentry, inode);
  742. err = 0;
  743. error:
  744. p9_fid_put(fid);
  745. v9fs_put_acl(dacl, pacl);
  746. p9_fid_put(dfid);
  747. return err;
  748. }
  749. /**
  750. * v9fs_vfs_get_link_dotl - follow a symlink path
  751. * @dentry: dentry for symlink
  752. * @inode: inode for symlink
  753. * @done: destructor for return value
  754. */
  755. static const char *
  756. v9fs_vfs_get_link_dotl(struct dentry *dentry,
  757. struct inode *inode,
  758. struct delayed_call *done)
  759. {
  760. struct p9_fid *fid;
  761. char *target;
  762. int retval;
  763. if (!dentry)
  764. return ERR_PTR(-ECHILD);
  765. p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
  766. fid = v9fs_fid_lookup(dentry);
  767. if (IS_ERR(fid))
  768. return ERR_CAST(fid);
  769. retval = p9_client_readlink(fid, &target);
  770. p9_fid_put(fid);
  771. if (retval)
  772. return ERR_PTR(retval);
  773. set_delayed_call(done, kfree_link, target);
  774. return target;
  775. }
  776. int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
  777. {
  778. struct p9_stat_dotl *st;
  779. struct v9fs_session_info *v9ses;
  780. unsigned int flags;
  781. v9ses = v9fs_inode2v9ses(inode);
  782. st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
  783. if (IS_ERR(st))
  784. return PTR_ERR(st);
  785. /*
  786. * Don't update inode if the file type is different
  787. */
  788. if (inode_wrong_type(inode, st->st_mode))
  789. goto out;
  790. /*
  791. * We don't want to refresh inode->i_size,
  792. * because we may have cached data
  793. */
  794. flags = (v9ses->cache & CACHE_LOOSE) ?
  795. V9FS_STAT2INODE_KEEP_ISIZE : 0;
  796. v9fs_stat2inode_dotl(st, inode, flags);
  797. out:
  798. kfree(st);
  799. return 0;
  800. }
  801. const struct inode_operations v9fs_dir_inode_operations_dotl = {
  802. .create = v9fs_vfs_create_dotl,
  803. .atomic_open = v9fs_vfs_atomic_open_dotl,
  804. .lookup = v9fs_vfs_lookup,
  805. .link = v9fs_vfs_link_dotl,
  806. .symlink = v9fs_vfs_symlink_dotl,
  807. .unlink = v9fs_vfs_unlink,
  808. .mkdir = v9fs_vfs_mkdir_dotl,
  809. .rmdir = v9fs_vfs_rmdir,
  810. .mknod = v9fs_vfs_mknod_dotl,
  811. .rename = v9fs_vfs_rename,
  812. .getattr = v9fs_vfs_getattr_dotl,
  813. .setattr = v9fs_vfs_setattr_dotl,
  814. .listxattr = v9fs_listxattr,
  815. .get_inode_acl = v9fs_iop_get_inode_acl,
  816. .get_acl = v9fs_iop_get_acl,
  817. .set_acl = v9fs_iop_set_acl,
  818. };
  819. const struct inode_operations v9fs_file_inode_operations_dotl = {
  820. .getattr = v9fs_vfs_getattr_dotl,
  821. .setattr = v9fs_vfs_setattr_dotl,
  822. .listxattr = v9fs_listxattr,
  823. .get_inode_acl = v9fs_iop_get_inode_acl,
  824. .get_acl = v9fs_iop_get_acl,
  825. .set_acl = v9fs_iop_set_acl,
  826. };
  827. const struct inode_operations v9fs_symlink_inode_operations_dotl = {
  828. .get_link = v9fs_vfs_get_link_dotl,
  829. .getattr = v9fs_vfs_getattr_dotl,
  830. .setattr = v9fs_vfs_setattr_dotl,
  831. .listxattr = v9fs_listxattr,
  832. };