vfs_inode_dotl.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. 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. v9fs_cache_inode_get_cookie(inode);
  127. retval = v9fs_get_acl(inode, fid);
  128. if (retval)
  129. goto error;
  130. unlock_new_inode(inode);
  131. return inode;
  132. error:
  133. unlock_new_inode(inode);
  134. iput(inode);
  135. return ERR_PTR(retval);
  136. }
  137. struct inode *
  138. v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  139. struct super_block *sb, int new)
  140. {
  141. struct p9_stat_dotl *st;
  142. struct inode *inode = NULL;
  143. st = p9_client_getattr_dotl(fid, P9_STATS_BASIC | P9_STATS_GEN);
  144. if (IS_ERR(st))
  145. return ERR_CAST(st);
  146. inode = v9fs_qid_iget_dotl(sb, &st->qid, fid, st, new);
  147. kfree(st);
  148. return inode;
  149. }
  150. struct dotl_openflag_map {
  151. int open_flag;
  152. int dotl_flag;
  153. };
  154. static int v9fs_mapped_dotl_flags(int flags)
  155. {
  156. int i;
  157. int rflags = 0;
  158. struct dotl_openflag_map dotl_oflag_map[] = {
  159. { O_CREAT, P9_DOTL_CREATE },
  160. { O_EXCL, P9_DOTL_EXCL },
  161. { O_NOCTTY, P9_DOTL_NOCTTY },
  162. { O_APPEND, P9_DOTL_APPEND },
  163. { O_NONBLOCK, P9_DOTL_NONBLOCK },
  164. { O_DSYNC, P9_DOTL_DSYNC },
  165. { FASYNC, P9_DOTL_FASYNC },
  166. { O_DIRECT, P9_DOTL_DIRECT },
  167. { O_LARGEFILE, P9_DOTL_LARGEFILE },
  168. { O_DIRECTORY, P9_DOTL_DIRECTORY },
  169. { O_NOFOLLOW, P9_DOTL_NOFOLLOW },
  170. { O_NOATIME, P9_DOTL_NOATIME },
  171. { O_CLOEXEC, P9_DOTL_CLOEXEC },
  172. { O_SYNC, P9_DOTL_SYNC},
  173. };
  174. for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
  175. if (flags & dotl_oflag_map[i].open_flag)
  176. rflags |= dotl_oflag_map[i].dotl_flag;
  177. }
  178. return rflags;
  179. }
  180. /**
  181. * v9fs_open_to_dotl_flags- convert Linux specific open flags to
  182. * plan 9 open flag.
  183. * @flags: flags to convert
  184. */
  185. int v9fs_open_to_dotl_flags(int flags)
  186. {
  187. int rflags = 0;
  188. /*
  189. * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
  190. * and P9_DOTL_NOACCESS
  191. */
  192. rflags |= flags & O_ACCMODE;
  193. rflags |= v9fs_mapped_dotl_flags(flags);
  194. return rflags;
  195. }
  196. /**
  197. * v9fs_vfs_create_dotl - VFS hook to create files for 9P2000.L protocol.
  198. * @dir: directory inode that is being created
  199. * @dentry: dentry that is being deleted
  200. * @mode: create permissions
  201. *
  202. */
  203. static int
  204. v9fs_vfs_create_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
  205. bool excl)
  206. {
  207. return v9fs_vfs_mknod_dotl(dir, dentry, omode, 0);
  208. }
  209. static int
  210. v9fs_vfs_atomic_open_dotl(struct inode *dir, struct dentry *dentry,
  211. struct file *file, unsigned flags, umode_t omode,
  212. int *opened)
  213. {
  214. int err = 0;
  215. kgid_t gid;
  216. umode_t mode;
  217. char *name = NULL;
  218. struct p9_qid qid;
  219. struct inode *inode;
  220. struct p9_fid *fid = NULL;
  221. struct v9fs_inode *v9inode;
  222. struct p9_fid *dfid, *ofid, *inode_fid;
  223. struct v9fs_session_info *v9ses;
  224. struct posix_acl *pacl = NULL, *dacl = NULL;
  225. struct dentry *res = NULL;
  226. if (d_unhashed(dentry)) {
  227. res = v9fs_vfs_lookup(dir, dentry, 0);
  228. if (IS_ERR(res))
  229. return PTR_ERR(res);
  230. if (res)
  231. dentry = res;
  232. }
  233. /* Only creates */
  234. if (!(flags & O_CREAT) || dentry->d_inode)
  235. return finish_no_open(file, res);
  236. v9ses = v9fs_inode2v9ses(dir);
  237. name = (char *) dentry->d_name.name;
  238. p9_debug(P9_DEBUG_VFS, "name:%s flags:0x%x mode:0x%hx\n",
  239. name, flags, omode);
  240. dfid = v9fs_fid_lookup(dentry->d_parent);
  241. if (IS_ERR(dfid)) {
  242. err = PTR_ERR(dfid);
  243. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  244. goto out;
  245. }
  246. /* clone a fid to use for creation */
  247. ofid = p9_client_walk(dfid, 0, NULL, 1);
  248. if (IS_ERR(ofid)) {
  249. err = PTR_ERR(ofid);
  250. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  251. goto out;
  252. }
  253. gid = v9fs_get_fsgid_for_create(dir);
  254. mode = omode;
  255. /* Update mode based on ACL value */
  256. err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
  257. if (err) {
  258. p9_debug(P9_DEBUG_VFS, "Failed to get acl values in creat %d\n",
  259. err);
  260. goto error;
  261. }
  262. err = p9_client_create_dotl(ofid, name, v9fs_open_to_dotl_flags(flags),
  263. mode, gid, &qid);
  264. if (err < 0) {
  265. p9_debug(P9_DEBUG_VFS, "p9_client_open_dotl failed in creat %d\n",
  266. err);
  267. goto error;
  268. }
  269. v9fs_invalidate_inode_attr(dir);
  270. /* instantiate inode and assign the unopened fid to the dentry */
  271. fid = p9_client_walk(dfid, 1, &name, 1);
  272. if (IS_ERR(fid)) {
  273. err = PTR_ERR(fid);
  274. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  275. fid = NULL;
  276. goto error;
  277. }
  278. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  279. if (IS_ERR(inode)) {
  280. err = PTR_ERR(inode);
  281. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n", err);
  282. goto error;
  283. }
  284. /* Now set the ACL based on the default value */
  285. v9fs_set_create_acl(inode, fid, dacl, pacl);
  286. v9fs_fid_add(dentry, fid);
  287. d_instantiate(dentry, inode);
  288. v9inode = V9FS_I(inode);
  289. mutex_lock(&v9inode->v_mutex);
  290. if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
  291. !v9inode->writeback_fid &&
  292. ((flags & O_ACCMODE) != O_RDONLY)) {
  293. /*
  294. * clone a fid and add it to writeback_fid
  295. * we do it during open time instead of
  296. * page dirty time via write_begin/page_mkwrite
  297. * because we want write after unlink usecase
  298. * to work.
  299. */
  300. inode_fid = v9fs_writeback_fid(dentry);
  301. if (IS_ERR(inode_fid)) {
  302. err = PTR_ERR(inode_fid);
  303. mutex_unlock(&v9inode->v_mutex);
  304. goto err_clunk_old_fid;
  305. }
  306. v9inode->writeback_fid = (void *) inode_fid;
  307. }
  308. mutex_unlock(&v9inode->v_mutex);
  309. /* Since we are opening a file, assign the open fid to the file */
  310. err = finish_open(file, dentry, generic_file_open, opened);
  311. if (err)
  312. goto err_clunk_old_fid;
  313. file->private_data = ofid;
  314. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  315. v9fs_cache_inode_set_cookie(inode, file);
  316. *opened |= FILE_CREATED;
  317. out:
  318. v9fs_put_acl(dacl, pacl);
  319. dput(res);
  320. return err;
  321. error:
  322. if (fid)
  323. p9_client_clunk(fid);
  324. err_clunk_old_fid:
  325. if (ofid)
  326. p9_client_clunk(ofid);
  327. goto out;
  328. }
  329. /**
  330. * v9fs_vfs_mkdir_dotl - VFS mkdir hook to create a directory
  331. * @dir: inode that is being unlinked
  332. * @dentry: dentry that is being unlinked
  333. * @mode: mode for new directory
  334. *
  335. */
  336. static int v9fs_vfs_mkdir_dotl(struct inode *dir,
  337. struct dentry *dentry, umode_t omode)
  338. {
  339. int err;
  340. struct v9fs_session_info *v9ses;
  341. struct p9_fid *fid = NULL, *dfid = NULL;
  342. kgid_t gid;
  343. char *name;
  344. umode_t mode;
  345. struct inode *inode;
  346. struct p9_qid qid;
  347. struct dentry *dir_dentry;
  348. struct posix_acl *dacl = NULL, *pacl = NULL;
  349. p9_debug(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  350. err = 0;
  351. v9ses = v9fs_inode2v9ses(dir);
  352. omode |= S_IFDIR;
  353. if (dir->i_mode & S_ISGID)
  354. omode |= S_ISGID;
  355. dir_dentry = dentry->d_parent;
  356. dfid = v9fs_fid_lookup(dir_dentry);
  357. if (IS_ERR(dfid)) {
  358. err = PTR_ERR(dfid);
  359. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  360. dfid = NULL;
  361. goto error;
  362. }
  363. gid = v9fs_get_fsgid_for_create(dir);
  364. mode = omode;
  365. /* Update mode based on ACL value */
  366. err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
  367. if (err) {
  368. p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mkdir %d\n",
  369. err);
  370. goto error;
  371. }
  372. name = (char *) dentry->d_name.name;
  373. err = p9_client_mkdir_dotl(dfid, name, mode, gid, &qid);
  374. if (err < 0)
  375. goto error;
  376. fid = p9_client_walk(dfid, 1, &name, 1);
  377. if (IS_ERR(fid)) {
  378. err = PTR_ERR(fid);
  379. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  380. err);
  381. fid = NULL;
  382. goto error;
  383. }
  384. /* instantiate inode and assign the unopened fid to the dentry */
  385. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  386. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  387. if (IS_ERR(inode)) {
  388. err = PTR_ERR(inode);
  389. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
  390. err);
  391. goto error;
  392. }
  393. v9fs_fid_add(dentry, fid);
  394. v9fs_set_create_acl(inode, fid, dacl, pacl);
  395. d_instantiate(dentry, inode);
  396. fid = NULL;
  397. err = 0;
  398. } else {
  399. /*
  400. * Not in cached mode. No need to populate
  401. * inode with stat. We need to get an inode
  402. * so that we can set the acl with dentry
  403. */
  404. inode = v9fs_get_inode(dir->i_sb, mode, 0);
  405. if (IS_ERR(inode)) {
  406. err = PTR_ERR(inode);
  407. goto error;
  408. }
  409. v9fs_set_create_acl(inode, fid, dacl, pacl);
  410. d_instantiate(dentry, inode);
  411. }
  412. inc_nlink(dir);
  413. v9fs_invalidate_inode_attr(dir);
  414. error:
  415. if (fid)
  416. p9_client_clunk(fid);
  417. v9fs_put_acl(dacl, pacl);
  418. return err;
  419. }
  420. static int
  421. v9fs_vfs_getattr_dotl(struct vfsmount *mnt, struct dentry *dentry,
  422. struct kstat *stat)
  423. {
  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(dentry->d_inode, 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, dentry->d_inode);
  443. generic_fillattr(dentry->d_inode, 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 = dentry->d_inode;
  498. p9_debug(P9_DEBUG_VFS, "\n");
  499. retval = inode_change_ok(inode, 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. * @sb: superblock of filesystem
  539. *
  540. */
  541. void
  542. v9fs_stat2inode_dotl(struct p9_stat_dotl *stat, struct inode *inode)
  543. {
  544. umode_t mode;
  545. struct v9fs_inode *v9inode = V9FS_I(inode);
  546. if ((stat->st_result_mask & P9_STATS_BASIC) == P9_STATS_BASIC) {
  547. inode->i_atime.tv_sec = stat->st_atime_sec;
  548. inode->i_atime.tv_nsec = stat->st_atime_nsec;
  549. inode->i_mtime.tv_sec = stat->st_mtime_sec;
  550. inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
  551. inode->i_ctime.tv_sec = stat->st_ctime_sec;
  552. inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
  553. inode->i_uid = stat->st_uid;
  554. inode->i_gid = stat->st_gid;
  555. set_nlink(inode, stat->st_nlink);
  556. mode = stat->st_mode & S_IALLUGO;
  557. mode |= inode->i_mode & ~S_IALLUGO;
  558. inode->i_mode = mode;
  559. i_size_write(inode, stat->st_size);
  560. inode->i_blocks = stat->st_blocks;
  561. } else {
  562. if (stat->st_result_mask & P9_STATS_ATIME) {
  563. inode->i_atime.tv_sec = stat->st_atime_sec;
  564. inode->i_atime.tv_nsec = stat->st_atime_nsec;
  565. }
  566. if (stat->st_result_mask & P9_STATS_MTIME) {
  567. inode->i_mtime.tv_sec = stat->st_mtime_sec;
  568. inode->i_mtime.tv_nsec = stat->st_mtime_nsec;
  569. }
  570. if (stat->st_result_mask & P9_STATS_CTIME) {
  571. inode->i_ctime.tv_sec = stat->st_ctime_sec;
  572. inode->i_ctime.tv_nsec = stat->st_ctime_nsec;
  573. }
  574. if (stat->st_result_mask & P9_STATS_UID)
  575. inode->i_uid = stat->st_uid;
  576. if (stat->st_result_mask & P9_STATS_GID)
  577. inode->i_gid = stat->st_gid;
  578. if (stat->st_result_mask & P9_STATS_NLINK)
  579. set_nlink(inode, stat->st_nlink);
  580. if (stat->st_result_mask & P9_STATS_MODE) {
  581. inode->i_mode = stat->st_mode;
  582. if ((S_ISBLK(inode->i_mode)) ||
  583. (S_ISCHR(inode->i_mode)))
  584. init_special_inode(inode, inode->i_mode,
  585. inode->i_rdev);
  586. }
  587. if (stat->st_result_mask & P9_STATS_RDEV)
  588. inode->i_rdev = new_decode_dev(stat->st_rdev);
  589. if (stat->st_result_mask & P9_STATS_SIZE)
  590. i_size_write(inode, stat->st_size);
  591. if (stat->st_result_mask & P9_STATS_BLOCKS)
  592. inode->i_blocks = stat->st_blocks;
  593. }
  594. if (stat->st_result_mask & P9_STATS_GEN)
  595. inode->i_generation = stat->st_gen;
  596. /* Currently we don't support P9_STATS_BTIME and P9_STATS_DATA_VERSION
  597. * because the inode structure does not have fields for them.
  598. */
  599. v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
  600. }
  601. static int
  602. v9fs_vfs_symlink_dotl(struct inode *dir, struct dentry *dentry,
  603. const char *symname)
  604. {
  605. int err;
  606. kgid_t gid;
  607. char *name;
  608. struct p9_qid qid;
  609. struct inode *inode;
  610. struct p9_fid *dfid;
  611. struct p9_fid *fid = NULL;
  612. struct v9fs_session_info *v9ses;
  613. name = (char *) dentry->d_name.name;
  614. p9_debug(P9_DEBUG_VFS, "%lu,%s,%s\n", dir->i_ino, name, symname);
  615. v9ses = v9fs_inode2v9ses(dir);
  616. dfid = v9fs_fid_lookup(dentry->d_parent);
  617. if (IS_ERR(dfid)) {
  618. err = PTR_ERR(dfid);
  619. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  620. return err;
  621. }
  622. gid = v9fs_get_fsgid_for_create(dir);
  623. /* Server doesn't alter fid on TSYMLINK. Hence no need to clone it. */
  624. err = p9_client_symlink(dfid, name, (char *)symname, gid, &qid);
  625. if (err < 0) {
  626. p9_debug(P9_DEBUG_VFS, "p9_client_symlink failed %d\n", err);
  627. goto error;
  628. }
  629. v9fs_invalidate_inode_attr(dir);
  630. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  631. /* Now walk from the parent so we can get an unopened fid. */
  632. fid = p9_client_walk(dfid, 1, &name, 1);
  633. if (IS_ERR(fid)) {
  634. err = PTR_ERR(fid);
  635. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  636. err);
  637. fid = NULL;
  638. goto error;
  639. }
  640. /* instantiate inode and assign the unopened fid to dentry */
  641. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  642. if (IS_ERR(inode)) {
  643. err = PTR_ERR(inode);
  644. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
  645. err);
  646. goto error;
  647. }
  648. v9fs_fid_add(dentry, fid);
  649. d_instantiate(dentry, inode);
  650. fid = NULL;
  651. err = 0;
  652. } else {
  653. /* Not in cached mode. No need to populate inode with stat */
  654. inode = v9fs_get_inode(dir->i_sb, S_IFLNK, 0);
  655. if (IS_ERR(inode)) {
  656. err = PTR_ERR(inode);
  657. goto error;
  658. }
  659. d_instantiate(dentry, inode);
  660. }
  661. error:
  662. if (fid)
  663. p9_client_clunk(fid);
  664. return err;
  665. }
  666. /**
  667. * v9fs_vfs_link_dotl - create a hardlink for dotl
  668. * @old_dentry: dentry for file to link to
  669. * @dir: inode destination for new link
  670. * @dentry: dentry for link
  671. *
  672. */
  673. static int
  674. v9fs_vfs_link_dotl(struct dentry *old_dentry, struct inode *dir,
  675. struct dentry *dentry)
  676. {
  677. int err;
  678. struct dentry *dir_dentry;
  679. struct p9_fid *dfid, *oldfid;
  680. struct v9fs_session_info *v9ses;
  681. p9_debug(P9_DEBUG_VFS, "dir ino: %lu, old_name: %s, new_name: %s\n",
  682. dir->i_ino, old_dentry->d_name.name, dentry->d_name.name);
  683. v9ses = v9fs_inode2v9ses(dir);
  684. dir_dentry = dentry->d_parent;
  685. dfid = v9fs_fid_lookup(dir_dentry);
  686. if (IS_ERR(dfid))
  687. return PTR_ERR(dfid);
  688. oldfid = v9fs_fid_lookup(old_dentry);
  689. if (IS_ERR(oldfid))
  690. return PTR_ERR(oldfid);
  691. err = p9_client_link(dfid, oldfid, (char *)dentry->d_name.name);
  692. if (err < 0) {
  693. p9_debug(P9_DEBUG_VFS, "p9_client_link failed %d\n", err);
  694. return err;
  695. }
  696. v9fs_invalidate_inode_attr(dir);
  697. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  698. /* Get the latest stat info from server. */
  699. struct p9_fid *fid;
  700. fid = v9fs_fid_lookup(old_dentry);
  701. if (IS_ERR(fid))
  702. return PTR_ERR(fid);
  703. v9fs_refresh_inode_dotl(fid, old_dentry->d_inode);
  704. }
  705. ihold(old_dentry->d_inode);
  706. d_instantiate(dentry, old_dentry->d_inode);
  707. return err;
  708. }
  709. /**
  710. * v9fs_vfs_mknod_dotl - create a special file
  711. * @dir: inode destination for new link
  712. * @dentry: dentry for file
  713. * @mode: mode for creation
  714. * @rdev: device associated with special file
  715. *
  716. */
  717. static int
  718. v9fs_vfs_mknod_dotl(struct inode *dir, struct dentry *dentry, umode_t omode,
  719. dev_t rdev)
  720. {
  721. int err;
  722. kgid_t gid;
  723. char *name;
  724. umode_t mode;
  725. struct v9fs_session_info *v9ses;
  726. struct p9_fid *fid = NULL, *dfid = NULL;
  727. struct inode *inode;
  728. struct p9_qid qid;
  729. struct dentry *dir_dentry;
  730. struct posix_acl *dacl = NULL, *pacl = NULL;
  731. p9_debug(P9_DEBUG_VFS, " %lu,%s mode: %hx MAJOR: %u MINOR: %u\n",
  732. dir->i_ino, dentry->d_name.name, omode,
  733. MAJOR(rdev), MINOR(rdev));
  734. if (!new_valid_dev(rdev))
  735. return -EINVAL;
  736. v9ses = v9fs_inode2v9ses(dir);
  737. dir_dentry = dentry->d_parent;
  738. dfid = v9fs_fid_lookup(dir_dentry);
  739. if (IS_ERR(dfid)) {
  740. err = PTR_ERR(dfid);
  741. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  742. dfid = NULL;
  743. goto error;
  744. }
  745. gid = v9fs_get_fsgid_for_create(dir);
  746. mode = omode;
  747. /* Update mode based on ACL value */
  748. err = v9fs_acl_mode(dir, &mode, &dacl, &pacl);
  749. if (err) {
  750. p9_debug(P9_DEBUG_VFS, "Failed to get acl values in mknod %d\n",
  751. err);
  752. goto error;
  753. }
  754. name = (char *) dentry->d_name.name;
  755. err = p9_client_mknod_dotl(dfid, name, mode, rdev, gid, &qid);
  756. if (err < 0)
  757. goto error;
  758. v9fs_invalidate_inode_attr(dir);
  759. fid = p9_client_walk(dfid, 1, &name, 1);
  760. if (IS_ERR(fid)) {
  761. err = PTR_ERR(fid);
  762. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n",
  763. err);
  764. fid = NULL;
  765. goto error;
  766. }
  767. /* instantiate inode and assign the unopened fid to the dentry */
  768. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  769. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  770. if (IS_ERR(inode)) {
  771. err = PTR_ERR(inode);
  772. p9_debug(P9_DEBUG_VFS, "inode creation failed %d\n",
  773. err);
  774. goto error;
  775. }
  776. v9fs_set_create_acl(inode, fid, dacl, pacl);
  777. v9fs_fid_add(dentry, fid);
  778. d_instantiate(dentry, inode);
  779. fid = NULL;
  780. err = 0;
  781. } else {
  782. /*
  783. * Not in cached mode. No need to populate inode with stat.
  784. * socket syscall returns a fd, so we need instantiate
  785. */
  786. inode = v9fs_get_inode(dir->i_sb, mode, rdev);
  787. if (IS_ERR(inode)) {
  788. err = PTR_ERR(inode);
  789. goto error;
  790. }
  791. v9fs_set_create_acl(inode, fid, dacl, pacl);
  792. d_instantiate(dentry, inode);
  793. }
  794. error:
  795. if (fid)
  796. p9_client_clunk(fid);
  797. v9fs_put_acl(dacl, pacl);
  798. return err;
  799. }
  800. /**
  801. * v9fs_vfs_follow_link_dotl - follow a symlink path
  802. * @dentry: dentry for symlink
  803. * @nd: nameidata
  804. *
  805. */
  806. static void *
  807. v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
  808. {
  809. int retval;
  810. struct p9_fid *fid;
  811. char *link = __getname();
  812. char *target;
  813. p9_debug(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
  814. if (!link) {
  815. link = ERR_PTR(-ENOMEM);
  816. goto ndset;
  817. }
  818. fid = v9fs_fid_lookup(dentry);
  819. if (IS_ERR(fid)) {
  820. __putname(link);
  821. link = ERR_CAST(fid);
  822. goto ndset;
  823. }
  824. retval = p9_client_readlink(fid, &target);
  825. if (!retval) {
  826. strcpy(link, target);
  827. kfree(target);
  828. goto ndset;
  829. }
  830. __putname(link);
  831. link = ERR_PTR(retval);
  832. ndset:
  833. nd_set_link(nd, link);
  834. return NULL;
  835. }
  836. int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
  837. {
  838. loff_t i_size;
  839. struct p9_stat_dotl *st;
  840. struct v9fs_session_info *v9ses;
  841. v9ses = v9fs_inode2v9ses(inode);
  842. st = p9_client_getattr_dotl(fid, P9_STATS_ALL);
  843. if (IS_ERR(st))
  844. return PTR_ERR(st);
  845. /*
  846. * Don't update inode if the file type is different
  847. */
  848. if ((inode->i_mode & S_IFMT) != (st->st_mode & S_IFMT))
  849. goto out;
  850. spin_lock(&inode->i_lock);
  851. /*
  852. * We don't want to refresh inode->i_size,
  853. * because we may have cached data
  854. */
  855. i_size = inode->i_size;
  856. v9fs_stat2inode_dotl(st, inode);
  857. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  858. inode->i_size = i_size;
  859. spin_unlock(&inode->i_lock);
  860. out:
  861. kfree(st);
  862. return 0;
  863. }
  864. const struct inode_operations v9fs_dir_inode_operations_dotl = {
  865. .create = v9fs_vfs_create_dotl,
  866. .atomic_open = v9fs_vfs_atomic_open_dotl,
  867. .lookup = v9fs_vfs_lookup,
  868. .link = v9fs_vfs_link_dotl,
  869. .symlink = v9fs_vfs_symlink_dotl,
  870. .unlink = v9fs_vfs_unlink,
  871. .mkdir = v9fs_vfs_mkdir_dotl,
  872. .rmdir = v9fs_vfs_rmdir,
  873. .mknod = v9fs_vfs_mknod_dotl,
  874. .rename = v9fs_vfs_rename,
  875. .getattr = v9fs_vfs_getattr_dotl,
  876. .setattr = v9fs_vfs_setattr_dotl,
  877. .setxattr = generic_setxattr,
  878. .getxattr = generic_getxattr,
  879. .removexattr = generic_removexattr,
  880. .listxattr = v9fs_listxattr,
  881. .get_acl = v9fs_iop_get_acl,
  882. };
  883. const struct inode_operations v9fs_file_inode_operations_dotl = {
  884. .getattr = v9fs_vfs_getattr_dotl,
  885. .setattr = v9fs_vfs_setattr_dotl,
  886. .setxattr = generic_setxattr,
  887. .getxattr = generic_getxattr,
  888. .removexattr = generic_removexattr,
  889. .listxattr = v9fs_listxattr,
  890. .get_acl = v9fs_iop_get_acl,
  891. };
  892. const struct inode_operations v9fs_symlink_inode_operations_dotl = {
  893. .readlink = generic_readlink,
  894. .follow_link = v9fs_vfs_follow_link_dotl,
  895. .put_link = v9fs_vfs_put_link,
  896. .getattr = v9fs_vfs_getattr_dotl,
  897. .setattr = v9fs_vfs_setattr_dotl,
  898. .setxattr = generic_setxattr,
  899. .getxattr = generic_getxattr,
  900. .removexattr = generic_removexattr,
  901. .listxattr = v9fs_listxattr,
  902. };