vfs_inode_dotl.c 26 KB

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