vfs_inode_dotl.c 25 KB

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