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