vfs_inode_dotl.c 25 KB

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