vfs_inode_dotl.c 23 KB

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