vfs_inode_dotl.c 24 KB

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