vfs_inode_dotl.c 25 KB

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