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