vfs_inode_dotl.c 21 KB

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