vfs_inode_dotl.c 25 KB

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