vfs_inode_dotl.c 25 KB

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