vfs_inode_dotl.c 25 KB

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