vfs_inode.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. /*
  2. * linux/fs/9p/vfs_inode.c
  3. *
  4. * This file contains vfs inode ops for the 9P2000 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/file.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/stat.h>
  32. #include <linux/string.h>
  33. #include <linux/inet.h>
  34. #include <linux/namei.h>
  35. #include <linux/idr.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/xattr.h>
  39. #include <linux/posix_acl.h>
  40. #include <net/9p/9p.h>
  41. #include <net/9p/client.h>
  42. #include "v9fs.h"
  43. #include "v9fs_vfs.h"
  44. #include "fid.h"
  45. #include "cache.h"
  46. #include "xattr.h"
  47. #include "acl.h"
  48. static const struct inode_operations v9fs_dir_inode_operations;
  49. static const struct inode_operations v9fs_dir_inode_operations_dotu;
  50. static const struct inode_operations v9fs_file_inode_operations;
  51. static const struct inode_operations v9fs_symlink_inode_operations;
  52. /**
  53. * unixmode2p9mode - convert unix mode bits to plan 9
  54. * @v9ses: v9fs session information
  55. * @mode: mode to convert
  56. *
  57. */
  58. static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
  59. {
  60. int res;
  61. res = mode & 0777;
  62. if (S_ISDIR(mode))
  63. res |= P9_DMDIR;
  64. if (v9fs_proto_dotu(v9ses)) {
  65. if (v9ses->nodev == 0) {
  66. if (S_ISSOCK(mode))
  67. res |= P9_DMSOCKET;
  68. if (S_ISFIFO(mode))
  69. res |= P9_DMNAMEDPIPE;
  70. if (S_ISBLK(mode))
  71. res |= P9_DMDEVICE;
  72. if (S_ISCHR(mode))
  73. res |= P9_DMDEVICE;
  74. }
  75. if ((mode & S_ISUID) == S_ISUID)
  76. res |= P9_DMSETUID;
  77. if ((mode & S_ISGID) == S_ISGID)
  78. res |= P9_DMSETGID;
  79. if ((mode & S_ISVTX) == S_ISVTX)
  80. res |= P9_DMSETVTX;
  81. }
  82. return res;
  83. }
  84. /**
  85. * p9mode2perm- convert plan9 mode bits to unix permission bits
  86. * @v9ses: v9fs session information
  87. * @stat: p9_wstat from which mode need to be derived
  88. *
  89. */
  90. static int p9mode2perm(struct v9fs_session_info *v9ses,
  91. struct p9_wstat *stat)
  92. {
  93. int res;
  94. int mode = stat->mode;
  95. res = mode & S_IALLUGO;
  96. if (v9fs_proto_dotu(v9ses)) {
  97. if ((mode & P9_DMSETUID) == P9_DMSETUID)
  98. res |= S_ISUID;
  99. if ((mode & P9_DMSETGID) == P9_DMSETGID)
  100. res |= S_ISGID;
  101. if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
  102. res |= S_ISVTX;
  103. }
  104. return res;
  105. }
  106. /**
  107. * p9mode2unixmode- convert plan9 mode bits to unix mode bits
  108. * @v9ses: v9fs session information
  109. * @stat: p9_wstat from which mode need to be derived
  110. * @rdev: major number, minor number in case of device files.
  111. *
  112. */
  113. static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
  114. struct p9_wstat *stat, dev_t *rdev)
  115. {
  116. int res;
  117. u32 mode = stat->mode;
  118. *rdev = 0;
  119. res = p9mode2perm(v9ses, stat);
  120. if ((mode & P9_DMDIR) == P9_DMDIR)
  121. res |= S_IFDIR;
  122. else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
  123. res |= S_IFLNK;
  124. else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
  125. && (v9ses->nodev == 0))
  126. res |= S_IFSOCK;
  127. else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
  128. && (v9ses->nodev == 0))
  129. res |= S_IFIFO;
  130. else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
  131. && (v9ses->nodev == 0)) {
  132. char type = 0, ext[32];
  133. int major = -1, minor = -1;
  134. strlcpy(ext, stat->extension, sizeof(ext));
  135. sscanf(ext, "%c %i %i", &type, &major, &minor);
  136. switch (type) {
  137. case 'c':
  138. res |= S_IFCHR;
  139. break;
  140. case 'b':
  141. res |= S_IFBLK;
  142. break;
  143. default:
  144. p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
  145. type, stat->extension);
  146. };
  147. *rdev = MKDEV(major, minor);
  148. } else
  149. res |= S_IFREG;
  150. return res;
  151. }
  152. /**
  153. * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
  154. * @uflags: flags to convert
  155. * @extended: if .u extensions are active
  156. */
  157. int v9fs_uflags2omode(int uflags, int extended)
  158. {
  159. int ret;
  160. ret = 0;
  161. switch (uflags&3) {
  162. default:
  163. case O_RDONLY:
  164. ret = P9_OREAD;
  165. break;
  166. case O_WRONLY:
  167. ret = P9_OWRITE;
  168. break;
  169. case O_RDWR:
  170. ret = P9_ORDWR;
  171. break;
  172. }
  173. if (extended) {
  174. if (uflags & O_EXCL)
  175. ret |= P9_OEXCL;
  176. if (uflags & O_APPEND)
  177. ret |= P9_OAPPEND;
  178. }
  179. return ret;
  180. }
  181. /**
  182. * v9fs_blank_wstat - helper function to setup a 9P stat structure
  183. * @wstat: structure to initialize
  184. *
  185. */
  186. void
  187. v9fs_blank_wstat(struct p9_wstat *wstat)
  188. {
  189. wstat->type = ~0;
  190. wstat->dev = ~0;
  191. wstat->qid.type = ~0;
  192. wstat->qid.version = ~0;
  193. *((long long *)&wstat->qid.path) = ~0;
  194. wstat->mode = ~0;
  195. wstat->atime = ~0;
  196. wstat->mtime = ~0;
  197. wstat->length = ~0;
  198. wstat->name = NULL;
  199. wstat->uid = NULL;
  200. wstat->gid = NULL;
  201. wstat->muid = NULL;
  202. wstat->n_uid = INVALID_UID;
  203. wstat->n_gid = INVALID_GID;
  204. wstat->n_muid = INVALID_UID;
  205. wstat->extension = NULL;
  206. }
  207. /**
  208. * v9fs_alloc_inode - helper function to allocate an inode
  209. *
  210. */
  211. struct inode *v9fs_alloc_inode(struct super_block *sb)
  212. {
  213. struct v9fs_inode *v9inode;
  214. v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
  215. GFP_KERNEL);
  216. if (!v9inode)
  217. return NULL;
  218. #ifdef CONFIG_9P_FSCACHE
  219. v9inode->fscache = NULL;
  220. spin_lock_init(&v9inode->fscache_lock);
  221. #endif
  222. v9inode->writeback_fid = NULL;
  223. v9inode->cache_validity = 0;
  224. mutex_init(&v9inode->v_mutex);
  225. return &v9inode->vfs_inode;
  226. }
  227. /**
  228. * v9fs_destroy_inode - destroy an inode
  229. *
  230. */
  231. static void v9fs_i_callback(struct rcu_head *head)
  232. {
  233. struct inode *inode = container_of(head, struct inode, i_rcu);
  234. kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
  235. }
  236. void v9fs_destroy_inode(struct inode *inode)
  237. {
  238. call_rcu(&inode->i_rcu, v9fs_i_callback);
  239. }
  240. int v9fs_init_inode(struct v9fs_session_info *v9ses,
  241. struct inode *inode, umode_t mode, dev_t rdev)
  242. {
  243. int err = 0;
  244. inode_init_owner(inode, NULL, mode);
  245. inode->i_blocks = 0;
  246. inode->i_rdev = rdev;
  247. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  248. inode->i_mapping->a_ops = &v9fs_addr_operations;
  249. switch (mode & S_IFMT) {
  250. case S_IFIFO:
  251. case S_IFBLK:
  252. case S_IFCHR:
  253. case S_IFSOCK:
  254. if (v9fs_proto_dotl(v9ses)) {
  255. inode->i_op = &v9fs_file_inode_operations_dotl;
  256. } else if (v9fs_proto_dotu(v9ses)) {
  257. inode->i_op = &v9fs_file_inode_operations;
  258. } else {
  259. p9_debug(P9_DEBUG_ERROR,
  260. "special files without extended mode\n");
  261. err = -EINVAL;
  262. goto error;
  263. }
  264. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  265. break;
  266. case S_IFREG:
  267. if (v9fs_proto_dotl(v9ses)) {
  268. inode->i_op = &v9fs_file_inode_operations_dotl;
  269. if (v9ses->cache == CACHE_LOOSE ||
  270. v9ses->cache == CACHE_FSCACHE)
  271. inode->i_fop =
  272. &v9fs_cached_file_operations_dotl;
  273. else if (v9ses->cache == CACHE_MMAP)
  274. inode->i_fop = &v9fs_mmap_file_operations_dotl;
  275. else
  276. inode->i_fop = &v9fs_file_operations_dotl;
  277. } else {
  278. inode->i_op = &v9fs_file_inode_operations;
  279. if (v9ses->cache == CACHE_LOOSE ||
  280. v9ses->cache == CACHE_FSCACHE)
  281. inode->i_fop =
  282. &v9fs_cached_file_operations;
  283. else if (v9ses->cache == CACHE_MMAP)
  284. inode->i_fop = &v9fs_mmap_file_operations;
  285. else
  286. inode->i_fop = &v9fs_file_operations;
  287. }
  288. break;
  289. case S_IFLNK:
  290. if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
  291. p9_debug(P9_DEBUG_ERROR,
  292. "extended modes used with legacy protocol\n");
  293. err = -EINVAL;
  294. goto error;
  295. }
  296. if (v9fs_proto_dotl(v9ses))
  297. inode->i_op = &v9fs_symlink_inode_operations_dotl;
  298. else
  299. inode->i_op = &v9fs_symlink_inode_operations;
  300. break;
  301. case S_IFDIR:
  302. inc_nlink(inode);
  303. if (v9fs_proto_dotl(v9ses))
  304. inode->i_op = &v9fs_dir_inode_operations_dotl;
  305. else if (v9fs_proto_dotu(v9ses))
  306. inode->i_op = &v9fs_dir_inode_operations_dotu;
  307. else
  308. inode->i_op = &v9fs_dir_inode_operations;
  309. if (v9fs_proto_dotl(v9ses))
  310. inode->i_fop = &v9fs_dir_operations_dotl;
  311. else
  312. inode->i_fop = &v9fs_dir_operations;
  313. break;
  314. default:
  315. p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
  316. mode, mode & S_IFMT);
  317. err = -EINVAL;
  318. goto error;
  319. }
  320. error:
  321. return err;
  322. }
  323. /**
  324. * v9fs_get_inode - helper function to setup an inode
  325. * @sb: superblock
  326. * @mode: mode to setup inode with
  327. *
  328. */
  329. struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  330. {
  331. int err;
  332. struct inode *inode;
  333. struct v9fs_session_info *v9ses = sb->s_fs_info;
  334. p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
  335. inode = new_inode(sb);
  336. if (!inode) {
  337. pr_warn("%s (%d): Problem allocating inode\n",
  338. __func__, task_pid_nr(current));
  339. return ERR_PTR(-ENOMEM);
  340. }
  341. err = v9fs_init_inode(v9ses, inode, mode, rdev);
  342. if (err) {
  343. iput(inode);
  344. return ERR_PTR(err);
  345. }
  346. return inode;
  347. }
  348. /*
  349. static struct v9fs_fid*
  350. v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
  351. {
  352. int err;
  353. int nfid;
  354. struct v9fs_fid *ret;
  355. struct v9fs_fcall *fcall;
  356. nfid = v9fs_get_idpool(&v9ses->fidpool);
  357. if (nfid < 0) {
  358. eprintk(KERN_WARNING, "no free fids available\n");
  359. return ERR_PTR(-ENOSPC);
  360. }
  361. err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
  362. &fcall);
  363. if (err < 0) {
  364. if (fcall && fcall->id == RWALK)
  365. goto clunk_fid;
  366. PRINT_FCALL_ERROR("walk error", fcall);
  367. v9fs_put_idpool(nfid, &v9ses->fidpool);
  368. goto error;
  369. }
  370. kfree(fcall);
  371. fcall = NULL;
  372. ret = v9fs_fid_create(v9ses, nfid);
  373. if (!ret) {
  374. err = -ENOMEM;
  375. goto clunk_fid;
  376. }
  377. err = v9fs_fid_insert(ret, dentry);
  378. if (err < 0) {
  379. v9fs_fid_destroy(ret);
  380. goto clunk_fid;
  381. }
  382. return ret;
  383. clunk_fid:
  384. v9fs_t_clunk(v9ses, nfid);
  385. error:
  386. kfree(fcall);
  387. return ERR_PTR(err);
  388. }
  389. */
  390. /**
  391. * v9fs_clear_inode - release an inode
  392. * @inode: inode to release
  393. *
  394. */
  395. void v9fs_evict_inode(struct inode *inode)
  396. {
  397. struct v9fs_inode *v9inode = V9FS_I(inode);
  398. truncate_inode_pages_final(inode->i_mapping);
  399. clear_inode(inode);
  400. filemap_fdatawrite(inode->i_mapping);
  401. v9fs_cache_inode_put_cookie(inode);
  402. /* clunk the fid stashed in writeback_fid */
  403. if (v9inode->writeback_fid) {
  404. p9_client_clunk(v9inode->writeback_fid);
  405. v9inode->writeback_fid = NULL;
  406. }
  407. }
  408. static int v9fs_test_inode(struct inode *inode, void *data)
  409. {
  410. int umode;
  411. dev_t rdev;
  412. struct v9fs_inode *v9inode = V9FS_I(inode);
  413. struct p9_wstat *st = (struct p9_wstat *)data;
  414. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  415. umode = p9mode2unixmode(v9ses, st, &rdev);
  416. /* don't match inode of different type */
  417. if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
  418. return 0;
  419. /* compare qid details */
  420. if (memcmp(&v9inode->qid.version,
  421. &st->qid.version, sizeof(v9inode->qid.version)))
  422. return 0;
  423. if (v9inode->qid.type != st->qid.type)
  424. return 0;
  425. return 1;
  426. }
  427. static int v9fs_test_new_inode(struct inode *inode, void *data)
  428. {
  429. return 0;
  430. }
  431. static int v9fs_set_inode(struct inode *inode, void *data)
  432. {
  433. struct v9fs_inode *v9inode = V9FS_I(inode);
  434. struct p9_wstat *st = (struct p9_wstat *)data;
  435. memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
  436. return 0;
  437. }
  438. static struct inode *v9fs_qid_iget(struct super_block *sb,
  439. struct p9_qid *qid,
  440. struct p9_wstat *st,
  441. int new)
  442. {
  443. dev_t rdev;
  444. int retval;
  445. umode_t umode;
  446. unsigned long i_ino;
  447. struct inode *inode;
  448. struct v9fs_session_info *v9ses = sb->s_fs_info;
  449. int (*test)(struct inode *, void *);
  450. if (new)
  451. test = v9fs_test_new_inode;
  452. else
  453. test = v9fs_test_inode;
  454. i_ino = v9fs_qid2ino(qid);
  455. inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
  456. if (!inode)
  457. return ERR_PTR(-ENOMEM);
  458. if (!(inode->i_state & I_NEW))
  459. return inode;
  460. /*
  461. * initialize the inode with the stat info
  462. * FIXME!! we may need support for stale inodes
  463. * later.
  464. */
  465. inode->i_ino = i_ino;
  466. umode = p9mode2unixmode(v9ses, st, &rdev);
  467. retval = v9fs_init_inode(v9ses, inode, umode, rdev);
  468. if (retval)
  469. goto error;
  470. v9fs_stat2inode(st, inode, sb);
  471. v9fs_cache_inode_get_cookie(inode);
  472. unlock_new_inode(inode);
  473. return inode;
  474. error:
  475. unlock_new_inode(inode);
  476. iput(inode);
  477. return ERR_PTR(retval);
  478. }
  479. struct inode *
  480. v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  481. struct super_block *sb, int new)
  482. {
  483. struct p9_wstat *st;
  484. struct inode *inode = NULL;
  485. st = p9_client_stat(fid);
  486. if (IS_ERR(st))
  487. return ERR_CAST(st);
  488. inode = v9fs_qid_iget(sb, &st->qid, st, new);
  489. p9stat_free(st);
  490. kfree(st);
  491. return inode;
  492. }
  493. /**
  494. * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
  495. * plan 9 AT flag.
  496. * @flags: flags to convert
  497. */
  498. static int v9fs_at_to_dotl_flags(int flags)
  499. {
  500. int rflags = 0;
  501. if (flags & AT_REMOVEDIR)
  502. rflags |= P9_DOTL_AT_REMOVEDIR;
  503. return rflags;
  504. }
  505. /**
  506. * v9fs_remove - helper function to remove files and directories
  507. * @dir: directory inode that is being deleted
  508. * @dentry: dentry that is being deleted
  509. * @flags: removing a directory
  510. *
  511. */
  512. static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
  513. {
  514. struct inode *inode;
  515. int retval = -EOPNOTSUPP;
  516. struct p9_fid *v9fid, *dfid;
  517. struct v9fs_session_info *v9ses;
  518. p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
  519. dir, dentry, flags);
  520. v9ses = v9fs_inode2v9ses(dir);
  521. inode = d_inode(dentry);
  522. dfid = v9fs_fid_lookup(dentry->d_parent);
  523. if (IS_ERR(dfid)) {
  524. retval = PTR_ERR(dfid);
  525. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
  526. return retval;
  527. }
  528. if (v9fs_proto_dotl(v9ses))
  529. retval = p9_client_unlinkat(dfid, dentry->d_name.name,
  530. v9fs_at_to_dotl_flags(flags));
  531. if (retval == -EOPNOTSUPP) {
  532. /* Try the one based on path */
  533. v9fid = v9fs_fid_clone(dentry);
  534. if (IS_ERR(v9fid))
  535. return PTR_ERR(v9fid);
  536. retval = p9_client_remove(v9fid);
  537. }
  538. if (!retval) {
  539. /*
  540. * directories on unlink should have zero
  541. * link count
  542. */
  543. if (flags & AT_REMOVEDIR) {
  544. clear_nlink(inode);
  545. drop_nlink(dir);
  546. } else
  547. drop_nlink(inode);
  548. v9fs_invalidate_inode_attr(inode);
  549. v9fs_invalidate_inode_attr(dir);
  550. }
  551. return retval;
  552. }
  553. /**
  554. * v9fs_create - Create a file
  555. * @v9ses: session information
  556. * @dir: directory that dentry is being created in
  557. * @dentry: dentry that is being created
  558. * @extension: 9p2000.u extension string to support devices, etc.
  559. * @perm: create permissions
  560. * @mode: open mode
  561. *
  562. */
  563. static struct p9_fid *
  564. v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
  565. struct dentry *dentry, char *extension, u32 perm, u8 mode)
  566. {
  567. int err;
  568. char *name;
  569. struct p9_fid *dfid, *ofid, *fid;
  570. struct inode *inode;
  571. p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
  572. err = 0;
  573. ofid = NULL;
  574. fid = NULL;
  575. name = (char *) dentry->d_name.name;
  576. dfid = v9fs_fid_lookup(dentry->d_parent);
  577. if (IS_ERR(dfid)) {
  578. err = PTR_ERR(dfid);
  579. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  580. return ERR_PTR(err);
  581. }
  582. /* clone a fid to use for creation */
  583. ofid = p9_client_walk(dfid, 0, NULL, 1);
  584. if (IS_ERR(ofid)) {
  585. err = PTR_ERR(ofid);
  586. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  587. return ERR_PTR(err);
  588. }
  589. err = p9_client_fcreate(ofid, name, perm, mode, extension);
  590. if (err < 0) {
  591. p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
  592. goto error;
  593. }
  594. if (!(perm & P9_DMLINK)) {
  595. /* now walk from the parent so we can get unopened fid */
  596. fid = p9_client_walk(dfid, 1, &name, 1);
  597. if (IS_ERR(fid)) {
  598. err = PTR_ERR(fid);
  599. p9_debug(P9_DEBUG_VFS,
  600. "p9_client_walk failed %d\n", err);
  601. fid = NULL;
  602. goto error;
  603. }
  604. /*
  605. * instantiate inode and assign the unopened fid to the dentry
  606. */
  607. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  608. if (IS_ERR(inode)) {
  609. err = PTR_ERR(inode);
  610. p9_debug(P9_DEBUG_VFS,
  611. "inode creation failed %d\n", err);
  612. goto error;
  613. }
  614. v9fs_fid_add(dentry, fid);
  615. d_instantiate(dentry, inode);
  616. }
  617. return ofid;
  618. error:
  619. if (ofid)
  620. p9_client_clunk(ofid);
  621. if (fid)
  622. p9_client_clunk(fid);
  623. return ERR_PTR(err);
  624. }
  625. /**
  626. * v9fs_vfs_create - VFS hook to create a regular file
  627. *
  628. * open(.., O_CREAT) is handled in v9fs_vfs_atomic_open(). This is only called
  629. * for mknod(2).
  630. *
  631. * @dir: directory inode that is being created
  632. * @dentry: dentry that is being deleted
  633. * @mode: create permissions
  634. *
  635. */
  636. static int
  637. v9fs_vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  638. bool excl)
  639. {
  640. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
  641. u32 perm = unixmode2p9mode(v9ses, mode);
  642. struct p9_fid *fid;
  643. /* P9_OEXCL? */
  644. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_ORDWR);
  645. if (IS_ERR(fid))
  646. return PTR_ERR(fid);
  647. v9fs_invalidate_inode_attr(dir);
  648. p9_client_clunk(fid);
  649. return 0;
  650. }
  651. /**
  652. * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
  653. * @dir: inode that is being unlinked
  654. * @dentry: dentry that is being unlinked
  655. * @mode: mode for new directory
  656. *
  657. */
  658. static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  659. {
  660. int err;
  661. u32 perm;
  662. struct p9_fid *fid;
  663. struct v9fs_session_info *v9ses;
  664. p9_debug(P9_DEBUG_VFS, "name %pd\n", dentry);
  665. err = 0;
  666. v9ses = v9fs_inode2v9ses(dir);
  667. perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
  668. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
  669. if (IS_ERR(fid)) {
  670. err = PTR_ERR(fid);
  671. fid = NULL;
  672. } else {
  673. inc_nlink(dir);
  674. v9fs_invalidate_inode_attr(dir);
  675. }
  676. if (fid)
  677. p9_client_clunk(fid);
  678. return err;
  679. }
  680. /**
  681. * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
  682. * @dir: inode that is being walked from
  683. * @dentry: dentry that is being walked to?
  684. * @flags: lookup flags (unused)
  685. *
  686. */
  687. struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
  688. unsigned int flags)
  689. {
  690. struct dentry *res;
  691. struct v9fs_session_info *v9ses;
  692. struct p9_fid *dfid, *fid;
  693. struct inode *inode;
  694. char *name;
  695. p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%pd) %p flags: %x\n",
  696. dir, dentry, dentry, flags);
  697. if (dentry->d_name.len > NAME_MAX)
  698. return ERR_PTR(-ENAMETOOLONG);
  699. v9ses = v9fs_inode2v9ses(dir);
  700. /* We can walk d_parent because we hold the dir->i_mutex */
  701. dfid = v9fs_fid_lookup(dentry->d_parent);
  702. if (IS_ERR(dfid))
  703. return ERR_CAST(dfid);
  704. name = (char *) dentry->d_name.name;
  705. fid = p9_client_walk(dfid, 1, &name, 1);
  706. if (IS_ERR(fid)) {
  707. if (fid == ERR_PTR(-ENOENT)) {
  708. d_add(dentry, NULL);
  709. return NULL;
  710. }
  711. return ERR_CAST(fid);
  712. }
  713. /*
  714. * Make sure we don't use a wrong inode due to parallel
  715. * unlink. For cached mode create calls request for new
  716. * inode. But with cache disabled, lookup should do this.
  717. */
  718. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  719. inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
  720. else
  721. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  722. if (IS_ERR(inode)) {
  723. p9_client_clunk(fid);
  724. return ERR_CAST(inode);
  725. }
  726. /*
  727. * If we had a rename on the server and a parallel lookup
  728. * for the new name, then make sure we instantiate with
  729. * the new name. ie look up for a/b, while on server somebody
  730. * moved b under k and client parallely did a lookup for
  731. * k/b.
  732. */
  733. res = d_splice_alias(inode, dentry);
  734. if (!res)
  735. v9fs_fid_add(dentry, fid);
  736. else if (!IS_ERR(res))
  737. v9fs_fid_add(res, fid);
  738. else
  739. p9_client_clunk(fid);
  740. return res;
  741. }
  742. static int
  743. v9fs_vfs_atomic_open(struct inode *dir, struct dentry *dentry,
  744. struct file *file, unsigned flags, umode_t mode,
  745. int *opened)
  746. {
  747. int err;
  748. u32 perm;
  749. struct v9fs_inode *v9inode;
  750. struct v9fs_session_info *v9ses;
  751. struct p9_fid *fid, *inode_fid;
  752. struct dentry *res = NULL;
  753. if (d_unhashed(dentry)) {
  754. res = v9fs_vfs_lookup(dir, dentry, 0);
  755. if (IS_ERR(res))
  756. return PTR_ERR(res);
  757. if (res)
  758. dentry = res;
  759. }
  760. /* Only creates */
  761. if (!(flags & O_CREAT) || d_really_is_positive(dentry))
  762. return finish_no_open(file, res);
  763. err = 0;
  764. v9ses = v9fs_inode2v9ses(dir);
  765. perm = unixmode2p9mode(v9ses, mode);
  766. fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
  767. v9fs_uflags2omode(flags,
  768. v9fs_proto_dotu(v9ses)));
  769. if (IS_ERR(fid)) {
  770. err = PTR_ERR(fid);
  771. fid = NULL;
  772. goto error;
  773. }
  774. v9fs_invalidate_inode_attr(dir);
  775. v9inode = V9FS_I(d_inode(dentry));
  776. mutex_lock(&v9inode->v_mutex);
  777. if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
  778. !v9inode->writeback_fid &&
  779. ((flags & O_ACCMODE) != O_RDONLY)) {
  780. /*
  781. * clone a fid and add it to writeback_fid
  782. * we do it during open time instead of
  783. * page dirty time via write_begin/page_mkwrite
  784. * because we want write after unlink usecase
  785. * to work.
  786. */
  787. inode_fid = v9fs_writeback_fid(dentry);
  788. if (IS_ERR(inode_fid)) {
  789. err = PTR_ERR(inode_fid);
  790. mutex_unlock(&v9inode->v_mutex);
  791. goto error;
  792. }
  793. v9inode->writeback_fid = (void *) inode_fid;
  794. }
  795. mutex_unlock(&v9inode->v_mutex);
  796. err = finish_open(file, dentry, generic_file_open, opened);
  797. if (err)
  798. goto error;
  799. file->private_data = fid;
  800. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  801. v9fs_cache_inode_set_cookie(d_inode(dentry), file);
  802. *opened |= FILE_CREATED;
  803. out:
  804. dput(res);
  805. return err;
  806. error:
  807. if (fid)
  808. p9_client_clunk(fid);
  809. goto out;
  810. }
  811. /**
  812. * v9fs_vfs_unlink - VFS unlink hook to delete an inode
  813. * @i: inode that is being unlinked
  814. * @d: dentry that is being unlinked
  815. *
  816. */
  817. int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
  818. {
  819. return v9fs_remove(i, d, 0);
  820. }
  821. /**
  822. * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
  823. * @i: inode that is being unlinked
  824. * @d: dentry that is being unlinked
  825. *
  826. */
  827. int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
  828. {
  829. return v9fs_remove(i, d, AT_REMOVEDIR);
  830. }
  831. /**
  832. * v9fs_vfs_rename - VFS hook to rename an inode
  833. * @old_dir: old dir inode
  834. * @old_dentry: old dentry
  835. * @new_dir: new dir inode
  836. * @new_dentry: new dentry
  837. *
  838. */
  839. int
  840. v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  841. struct inode *new_dir, struct dentry *new_dentry)
  842. {
  843. int retval;
  844. struct inode *old_inode;
  845. struct inode *new_inode;
  846. struct v9fs_session_info *v9ses;
  847. struct p9_fid *oldfid;
  848. struct p9_fid *olddirfid;
  849. struct p9_fid *newdirfid;
  850. struct p9_wstat wstat;
  851. p9_debug(P9_DEBUG_VFS, "\n");
  852. retval = 0;
  853. old_inode = d_inode(old_dentry);
  854. new_inode = d_inode(new_dentry);
  855. v9ses = v9fs_inode2v9ses(old_inode);
  856. oldfid = v9fs_fid_lookup(old_dentry);
  857. if (IS_ERR(oldfid))
  858. return PTR_ERR(oldfid);
  859. olddirfid = v9fs_fid_clone(old_dentry->d_parent);
  860. if (IS_ERR(olddirfid)) {
  861. retval = PTR_ERR(olddirfid);
  862. goto done;
  863. }
  864. newdirfid = v9fs_fid_clone(new_dentry->d_parent);
  865. if (IS_ERR(newdirfid)) {
  866. retval = PTR_ERR(newdirfid);
  867. goto clunk_olddir;
  868. }
  869. down_write(&v9ses->rename_sem);
  870. if (v9fs_proto_dotl(v9ses)) {
  871. retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
  872. newdirfid, new_dentry->d_name.name);
  873. if (retval == -EOPNOTSUPP)
  874. retval = p9_client_rename(oldfid, newdirfid,
  875. new_dentry->d_name.name);
  876. if (retval != -EOPNOTSUPP)
  877. goto clunk_newdir;
  878. }
  879. if (old_dentry->d_parent != new_dentry->d_parent) {
  880. /*
  881. * 9P .u can only handle file rename in the same directory
  882. */
  883. p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
  884. retval = -EXDEV;
  885. goto clunk_newdir;
  886. }
  887. v9fs_blank_wstat(&wstat);
  888. wstat.muid = v9ses->uname;
  889. wstat.name = (char *) new_dentry->d_name.name;
  890. retval = p9_client_wstat(oldfid, &wstat);
  891. clunk_newdir:
  892. if (!retval) {
  893. if (new_inode) {
  894. if (S_ISDIR(new_inode->i_mode))
  895. clear_nlink(new_inode);
  896. else
  897. drop_nlink(new_inode);
  898. }
  899. if (S_ISDIR(old_inode->i_mode)) {
  900. if (!new_inode)
  901. inc_nlink(new_dir);
  902. drop_nlink(old_dir);
  903. }
  904. v9fs_invalidate_inode_attr(old_inode);
  905. v9fs_invalidate_inode_attr(old_dir);
  906. v9fs_invalidate_inode_attr(new_dir);
  907. /* successful rename */
  908. d_move(old_dentry, new_dentry);
  909. }
  910. up_write(&v9ses->rename_sem);
  911. p9_client_clunk(newdirfid);
  912. clunk_olddir:
  913. p9_client_clunk(olddirfid);
  914. done:
  915. return retval;
  916. }
  917. /**
  918. * v9fs_vfs_getattr - retrieve file metadata
  919. * @mnt: mount information
  920. * @dentry: file to get attributes on
  921. * @stat: metadata structure to populate
  922. *
  923. */
  924. static int
  925. v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  926. struct kstat *stat)
  927. {
  928. struct v9fs_session_info *v9ses;
  929. struct p9_fid *fid;
  930. struct p9_wstat *st;
  931. p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  932. v9ses = v9fs_dentry2v9ses(dentry);
  933. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  934. generic_fillattr(d_inode(dentry), stat);
  935. return 0;
  936. }
  937. fid = v9fs_fid_lookup(dentry);
  938. if (IS_ERR(fid))
  939. return PTR_ERR(fid);
  940. st = p9_client_stat(fid);
  941. if (IS_ERR(st))
  942. return PTR_ERR(st);
  943. v9fs_stat2inode(st, d_inode(dentry), d_inode(dentry)->i_sb);
  944. generic_fillattr(d_inode(dentry), stat);
  945. p9stat_free(st);
  946. kfree(st);
  947. return 0;
  948. }
  949. /**
  950. * v9fs_vfs_setattr - set file metadata
  951. * @dentry: file whose metadata to set
  952. * @iattr: metadata assignment structure
  953. *
  954. */
  955. static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
  956. {
  957. int retval;
  958. struct v9fs_session_info *v9ses;
  959. struct p9_fid *fid;
  960. struct p9_wstat wstat;
  961. p9_debug(P9_DEBUG_VFS, "\n");
  962. retval = inode_change_ok(d_inode(dentry), iattr);
  963. if (retval)
  964. return retval;
  965. retval = -EPERM;
  966. v9ses = v9fs_dentry2v9ses(dentry);
  967. fid = v9fs_fid_lookup(dentry);
  968. if(IS_ERR(fid))
  969. return PTR_ERR(fid);
  970. v9fs_blank_wstat(&wstat);
  971. if (iattr->ia_valid & ATTR_MODE)
  972. wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
  973. if (iattr->ia_valid & ATTR_MTIME)
  974. wstat.mtime = iattr->ia_mtime.tv_sec;
  975. if (iattr->ia_valid & ATTR_ATIME)
  976. wstat.atime = iattr->ia_atime.tv_sec;
  977. if (iattr->ia_valid & ATTR_SIZE)
  978. wstat.length = iattr->ia_size;
  979. if (v9fs_proto_dotu(v9ses)) {
  980. if (iattr->ia_valid & ATTR_UID)
  981. wstat.n_uid = iattr->ia_uid;
  982. if (iattr->ia_valid & ATTR_GID)
  983. wstat.n_gid = iattr->ia_gid;
  984. }
  985. /* Write all dirty data */
  986. if (d_is_reg(dentry))
  987. filemap_write_and_wait(d_inode(dentry)->i_mapping);
  988. retval = p9_client_wstat(fid, &wstat);
  989. if (retval < 0)
  990. return retval;
  991. if ((iattr->ia_valid & ATTR_SIZE) &&
  992. iattr->ia_size != i_size_read(d_inode(dentry)))
  993. truncate_setsize(d_inode(dentry), iattr->ia_size);
  994. v9fs_invalidate_inode_attr(d_inode(dentry));
  995. setattr_copy(d_inode(dentry), iattr);
  996. mark_inode_dirty(d_inode(dentry));
  997. return 0;
  998. }
  999. /**
  1000. * v9fs_stat2inode - populate an inode structure with mistat info
  1001. * @stat: Plan 9 metadata (mistat) structure
  1002. * @inode: inode to populate
  1003. * @sb: superblock of filesystem
  1004. *
  1005. */
  1006. void
  1007. v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
  1008. struct super_block *sb)
  1009. {
  1010. umode_t mode;
  1011. char ext[32];
  1012. char tag_name[14];
  1013. unsigned int i_nlink;
  1014. struct v9fs_session_info *v9ses = sb->s_fs_info;
  1015. struct v9fs_inode *v9inode = V9FS_I(inode);
  1016. set_nlink(inode, 1);
  1017. inode->i_atime.tv_sec = stat->atime;
  1018. inode->i_mtime.tv_sec = stat->mtime;
  1019. inode->i_ctime.tv_sec = stat->mtime;
  1020. inode->i_uid = v9ses->dfltuid;
  1021. inode->i_gid = v9ses->dfltgid;
  1022. if (v9fs_proto_dotu(v9ses)) {
  1023. inode->i_uid = stat->n_uid;
  1024. inode->i_gid = stat->n_gid;
  1025. }
  1026. if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
  1027. if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
  1028. /*
  1029. * Hadlink support got added later to
  1030. * to the .u extension. So there can be
  1031. * server out there that doesn't support
  1032. * this even with .u extension. So check
  1033. * for non NULL stat->extension
  1034. */
  1035. strlcpy(ext, stat->extension, sizeof(ext));
  1036. /* HARDLINKCOUNT %u */
  1037. sscanf(ext, "%13s %u", tag_name, &i_nlink);
  1038. if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
  1039. set_nlink(inode, i_nlink);
  1040. }
  1041. }
  1042. mode = p9mode2perm(v9ses, stat);
  1043. mode |= inode->i_mode & ~S_IALLUGO;
  1044. inode->i_mode = mode;
  1045. i_size_write(inode, stat->length);
  1046. /* not real number of blocks, but 512 byte ones ... */
  1047. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  1048. v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
  1049. }
  1050. /**
  1051. * v9fs_qid2ino - convert qid into inode number
  1052. * @qid: qid to hash
  1053. *
  1054. * BUG: potential for inode number collisions?
  1055. */
  1056. ino_t v9fs_qid2ino(struct p9_qid *qid)
  1057. {
  1058. u64 path = qid->path + 2;
  1059. ino_t i = 0;
  1060. if (sizeof(ino_t) == sizeof(path))
  1061. memcpy(&i, &path, sizeof(ino_t));
  1062. else
  1063. i = (ino_t) (path ^ (path >> 32));
  1064. return i;
  1065. }
  1066. /**
  1067. * v9fs_vfs_follow_link - follow a symlink path
  1068. * @dentry: dentry for symlink
  1069. * @cookie: place to pass the data to put_link()
  1070. */
  1071. static const char *v9fs_vfs_follow_link(struct dentry *dentry, void **cookie)
  1072. {
  1073. struct v9fs_session_info *v9ses = v9fs_dentry2v9ses(dentry);
  1074. struct p9_fid *fid = v9fs_fid_lookup(dentry);
  1075. struct p9_wstat *st;
  1076. char *res;
  1077. p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
  1078. if (IS_ERR(fid))
  1079. return ERR_CAST(fid);
  1080. if (!v9fs_proto_dotu(v9ses))
  1081. return ERR_PTR(-EBADF);
  1082. st = p9_client_stat(fid);
  1083. if (IS_ERR(st))
  1084. return ERR_CAST(st);
  1085. if (!(st->mode & P9_DMSYMLINK)) {
  1086. p9stat_free(st);
  1087. kfree(st);
  1088. return ERR_PTR(-EINVAL);
  1089. }
  1090. res = st->extension;
  1091. st->extension = NULL;
  1092. if (strlen(res) >= PATH_MAX)
  1093. res[PATH_MAX - 1] = '\0';
  1094. p9stat_free(st);
  1095. kfree(st);
  1096. return *cookie = res;
  1097. }
  1098. /**
  1099. * v9fs_vfs_mkspecial - create a special file
  1100. * @dir: inode to create special file in
  1101. * @dentry: dentry to create
  1102. * @perm: mode to create special file
  1103. * @extension: 9p2000.u format extension string representing special file
  1104. *
  1105. */
  1106. static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
  1107. u32 perm, const char *extension)
  1108. {
  1109. struct p9_fid *fid;
  1110. struct v9fs_session_info *v9ses;
  1111. v9ses = v9fs_inode2v9ses(dir);
  1112. if (!v9fs_proto_dotu(v9ses)) {
  1113. p9_debug(P9_DEBUG_ERROR, "not extended\n");
  1114. return -EPERM;
  1115. }
  1116. fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
  1117. P9_OREAD);
  1118. if (IS_ERR(fid))
  1119. return PTR_ERR(fid);
  1120. v9fs_invalidate_inode_attr(dir);
  1121. p9_client_clunk(fid);
  1122. return 0;
  1123. }
  1124. /**
  1125. * v9fs_vfs_symlink - helper function to create symlinks
  1126. * @dir: directory inode containing symlink
  1127. * @dentry: dentry for symlink
  1128. * @symname: symlink data
  1129. *
  1130. * See Also: 9P2000.u RFC for more information
  1131. *
  1132. */
  1133. static int
  1134. v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1135. {
  1136. p9_debug(P9_DEBUG_VFS, " %lu,%pd,%s\n",
  1137. dir->i_ino, dentry, symname);
  1138. return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
  1139. }
  1140. #define U32_MAX_DIGITS 10
  1141. /**
  1142. * v9fs_vfs_link - create a hardlink
  1143. * @old_dentry: dentry for file to link to
  1144. * @dir: inode destination for new link
  1145. * @dentry: dentry for link
  1146. *
  1147. */
  1148. static int
  1149. v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
  1150. struct dentry *dentry)
  1151. {
  1152. int retval;
  1153. char name[1 + U32_MAX_DIGITS + 2]; /* sign + number + \n + \0 */
  1154. struct p9_fid *oldfid;
  1155. p9_debug(P9_DEBUG_VFS, " %lu,%pd,%pd\n",
  1156. dir->i_ino, dentry, old_dentry);
  1157. oldfid = v9fs_fid_clone(old_dentry);
  1158. if (IS_ERR(oldfid))
  1159. return PTR_ERR(oldfid);
  1160. sprintf(name, "%d\n", oldfid->fid);
  1161. retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
  1162. if (!retval) {
  1163. v9fs_refresh_inode(oldfid, d_inode(old_dentry));
  1164. v9fs_invalidate_inode_attr(dir);
  1165. }
  1166. p9_client_clunk(oldfid);
  1167. return retval;
  1168. }
  1169. /**
  1170. * v9fs_vfs_mknod - create a special file
  1171. * @dir: inode destination for new link
  1172. * @dentry: dentry for file
  1173. * @mode: mode for creation
  1174. * @rdev: device associated with special file
  1175. *
  1176. */
  1177. static int
  1178. v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  1179. {
  1180. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
  1181. int retval;
  1182. char name[2 + U32_MAX_DIGITS + 1 + U32_MAX_DIGITS + 1];
  1183. u32 perm;
  1184. p9_debug(P9_DEBUG_VFS, " %lu,%pd mode: %hx MAJOR: %u MINOR: %u\n",
  1185. dir->i_ino, dentry, mode,
  1186. MAJOR(rdev), MINOR(rdev));
  1187. if (!new_valid_dev(rdev))
  1188. return -EINVAL;
  1189. /* build extension */
  1190. if (S_ISBLK(mode))
  1191. sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
  1192. else if (S_ISCHR(mode))
  1193. sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
  1194. else
  1195. *name = 0;
  1196. perm = unixmode2p9mode(v9ses, mode);
  1197. retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
  1198. return retval;
  1199. }
  1200. int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
  1201. {
  1202. int umode;
  1203. dev_t rdev;
  1204. loff_t i_size;
  1205. struct p9_wstat *st;
  1206. struct v9fs_session_info *v9ses;
  1207. v9ses = v9fs_inode2v9ses(inode);
  1208. st = p9_client_stat(fid);
  1209. if (IS_ERR(st))
  1210. return PTR_ERR(st);
  1211. /*
  1212. * Don't update inode if the file type is different
  1213. */
  1214. umode = p9mode2unixmode(v9ses, st, &rdev);
  1215. if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
  1216. goto out;
  1217. spin_lock(&inode->i_lock);
  1218. /*
  1219. * We don't want to refresh inode->i_size,
  1220. * because we may have cached data
  1221. */
  1222. i_size = inode->i_size;
  1223. v9fs_stat2inode(st, inode, inode->i_sb);
  1224. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  1225. inode->i_size = i_size;
  1226. spin_unlock(&inode->i_lock);
  1227. out:
  1228. p9stat_free(st);
  1229. kfree(st);
  1230. return 0;
  1231. }
  1232. static const struct inode_operations v9fs_dir_inode_operations_dotu = {
  1233. .create = v9fs_vfs_create,
  1234. .lookup = v9fs_vfs_lookup,
  1235. .atomic_open = v9fs_vfs_atomic_open,
  1236. .symlink = v9fs_vfs_symlink,
  1237. .link = v9fs_vfs_link,
  1238. .unlink = v9fs_vfs_unlink,
  1239. .mkdir = v9fs_vfs_mkdir,
  1240. .rmdir = v9fs_vfs_rmdir,
  1241. .mknod = v9fs_vfs_mknod,
  1242. .rename = v9fs_vfs_rename,
  1243. .getattr = v9fs_vfs_getattr,
  1244. .setattr = v9fs_vfs_setattr,
  1245. };
  1246. static const struct inode_operations v9fs_dir_inode_operations = {
  1247. .create = v9fs_vfs_create,
  1248. .lookup = v9fs_vfs_lookup,
  1249. .atomic_open = v9fs_vfs_atomic_open,
  1250. .unlink = v9fs_vfs_unlink,
  1251. .mkdir = v9fs_vfs_mkdir,
  1252. .rmdir = v9fs_vfs_rmdir,
  1253. .mknod = v9fs_vfs_mknod,
  1254. .rename = v9fs_vfs_rename,
  1255. .getattr = v9fs_vfs_getattr,
  1256. .setattr = v9fs_vfs_setattr,
  1257. };
  1258. static const struct inode_operations v9fs_file_inode_operations = {
  1259. .getattr = v9fs_vfs_getattr,
  1260. .setattr = v9fs_vfs_setattr,
  1261. };
  1262. static const struct inode_operations v9fs_symlink_inode_operations = {
  1263. .readlink = generic_readlink,
  1264. .follow_link = v9fs_vfs_follow_link,
  1265. .put_link = kfree_put_link,
  1266. .getattr = v9fs_vfs_getattr,
  1267. .setattr = v9fs_vfs_setattr,
  1268. };