vfs_inode_dotl.c 25 KB

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