vfs_inode_dotl.c 25 KB

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