vfs_inode_dotl.c 25 KB

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