vfs_inode.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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. #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 <net/9p/9p.h>
  37. #include <net/9p/client.h>
  38. #include "v9fs.h"
  39. #include "v9fs_vfs.h"
  40. #include "fid.h"
  41. #include "cache.h"
  42. static const struct inode_operations v9fs_dir_inode_operations;
  43. static const struct inode_operations v9fs_dir_inode_operations_ext;
  44. static const struct inode_operations v9fs_file_inode_operations;
  45. static const struct inode_operations v9fs_symlink_inode_operations;
  46. /**
  47. * unixmode2p9mode - convert unix mode bits to plan 9
  48. * @v9ses: v9fs session information
  49. * @mode: mode to convert
  50. *
  51. */
  52. static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
  53. {
  54. int res;
  55. res = mode & 0777;
  56. if (S_ISDIR(mode))
  57. res |= P9_DMDIR;
  58. if (v9fs_proto_dotu(v9ses)) {
  59. if (S_ISLNK(mode))
  60. res |= P9_DMSYMLINK;
  61. if (v9ses->nodev == 0) {
  62. if (S_ISSOCK(mode))
  63. res |= P9_DMSOCKET;
  64. if (S_ISFIFO(mode))
  65. res |= P9_DMNAMEDPIPE;
  66. if (S_ISBLK(mode))
  67. res |= P9_DMDEVICE;
  68. if (S_ISCHR(mode))
  69. res |= P9_DMDEVICE;
  70. }
  71. if ((mode & S_ISUID) == S_ISUID)
  72. res |= P9_DMSETUID;
  73. if ((mode & S_ISGID) == S_ISGID)
  74. res |= P9_DMSETGID;
  75. if ((mode & S_ISVTX) == S_ISVTX)
  76. res |= P9_DMSETVTX;
  77. if ((mode & P9_DMLINK))
  78. res |= P9_DMLINK;
  79. }
  80. return res;
  81. }
  82. /**
  83. * p9mode2unixmode- convert plan9 mode bits to unix mode bits
  84. * @v9ses: v9fs session information
  85. * @mode: mode to convert
  86. *
  87. */
  88. static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
  89. {
  90. int res;
  91. res = mode & 0777;
  92. if ((mode & P9_DMDIR) == P9_DMDIR)
  93. res |= S_IFDIR;
  94. else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
  95. res |= S_IFLNK;
  96. else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
  97. && (v9ses->nodev == 0))
  98. res |= S_IFSOCK;
  99. else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
  100. && (v9ses->nodev == 0))
  101. res |= S_IFIFO;
  102. else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
  103. && (v9ses->nodev == 0))
  104. res |= S_IFBLK;
  105. else
  106. res |= S_IFREG;
  107. if (v9fs_proto_dotu(v9ses)) {
  108. if ((mode & P9_DMSETUID) == P9_DMSETUID)
  109. res |= S_ISUID;
  110. if ((mode & P9_DMSETGID) == P9_DMSETGID)
  111. res |= S_ISGID;
  112. if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
  113. res |= S_ISVTX;
  114. }
  115. return res;
  116. }
  117. /**
  118. * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
  119. * @uflags: flags to convert
  120. * @extended: if .u extensions are active
  121. */
  122. int v9fs_uflags2omode(int uflags, int extended)
  123. {
  124. int ret;
  125. ret = 0;
  126. switch (uflags&3) {
  127. default:
  128. case O_RDONLY:
  129. ret = P9_OREAD;
  130. break;
  131. case O_WRONLY:
  132. ret = P9_OWRITE;
  133. break;
  134. case O_RDWR:
  135. ret = P9_ORDWR;
  136. break;
  137. }
  138. if (uflags & O_TRUNC)
  139. ret |= P9_OTRUNC;
  140. if (extended) {
  141. if (uflags & O_EXCL)
  142. ret |= P9_OEXCL;
  143. if (uflags & O_APPEND)
  144. ret |= P9_OAPPEND;
  145. }
  146. return ret;
  147. }
  148. /**
  149. * v9fs_blank_wstat - helper function to setup a 9P stat structure
  150. * @wstat: structure to initialize
  151. *
  152. */
  153. void
  154. v9fs_blank_wstat(struct p9_wstat *wstat)
  155. {
  156. wstat->type = ~0;
  157. wstat->dev = ~0;
  158. wstat->qid.type = ~0;
  159. wstat->qid.version = ~0;
  160. *((long long *)&wstat->qid.path) = ~0;
  161. wstat->mode = ~0;
  162. wstat->atime = ~0;
  163. wstat->mtime = ~0;
  164. wstat->length = ~0;
  165. wstat->name = NULL;
  166. wstat->uid = NULL;
  167. wstat->gid = NULL;
  168. wstat->muid = NULL;
  169. wstat->n_uid = ~0;
  170. wstat->n_gid = ~0;
  171. wstat->n_muid = ~0;
  172. wstat->extension = NULL;
  173. }
  174. #ifdef CONFIG_9P_FSCACHE
  175. /**
  176. * v9fs_alloc_inode - helper function to allocate an inode
  177. * This callback is executed before setting up the inode so that we
  178. * can associate a vcookie with each inode.
  179. *
  180. */
  181. struct inode *v9fs_alloc_inode(struct super_block *sb)
  182. {
  183. struct v9fs_cookie *vcookie;
  184. vcookie = (struct v9fs_cookie *)kmem_cache_alloc(vcookie_cache,
  185. GFP_KERNEL);
  186. if (!vcookie)
  187. return NULL;
  188. vcookie->fscache = NULL;
  189. vcookie->qid = NULL;
  190. spin_lock_init(&vcookie->lock);
  191. return &vcookie->inode;
  192. }
  193. /**
  194. * v9fs_destroy_inode - destroy an inode
  195. *
  196. */
  197. void v9fs_destroy_inode(struct inode *inode)
  198. {
  199. kmem_cache_free(vcookie_cache, v9fs_inode2cookie(inode));
  200. }
  201. #endif
  202. /**
  203. * v9fs_get_inode - helper function to setup an inode
  204. * @sb: superblock
  205. * @mode: mode to setup inode with
  206. *
  207. */
  208. struct inode *v9fs_get_inode(struct super_block *sb, int mode)
  209. {
  210. int err;
  211. struct inode *inode;
  212. struct v9fs_session_info *v9ses = sb->s_fs_info;
  213. P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
  214. inode = new_inode(sb);
  215. if (!inode) {
  216. P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
  217. return ERR_PTR(-ENOMEM);
  218. }
  219. inode->i_mode = mode;
  220. inode->i_uid = current_fsuid();
  221. inode->i_gid = current_fsgid();
  222. inode->i_blocks = 0;
  223. inode->i_rdev = 0;
  224. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  225. inode->i_mapping->a_ops = &v9fs_addr_operations;
  226. switch (mode & S_IFMT) {
  227. case S_IFIFO:
  228. case S_IFBLK:
  229. case S_IFCHR:
  230. case S_IFSOCK:
  231. if (!v9fs_proto_dotu(v9ses)) {
  232. P9_DPRINTK(P9_DEBUG_ERROR,
  233. "special files without extended mode\n");
  234. err = -EINVAL;
  235. goto error;
  236. }
  237. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  238. break;
  239. case S_IFREG:
  240. inode->i_op = &v9fs_file_inode_operations;
  241. inode->i_fop = &v9fs_file_operations;
  242. break;
  243. case S_IFLNK:
  244. if (!v9fs_proto_dotu(v9ses)) {
  245. P9_DPRINTK(P9_DEBUG_ERROR,
  246. "extended modes used w/o 9P2000.u\n");
  247. err = -EINVAL;
  248. goto error;
  249. }
  250. inode->i_op = &v9fs_symlink_inode_operations;
  251. break;
  252. case S_IFDIR:
  253. inc_nlink(inode);
  254. if (v9fs_proto_dotu(v9ses))
  255. inode->i_op = &v9fs_dir_inode_operations_ext;
  256. else
  257. inode->i_op = &v9fs_dir_inode_operations;
  258. inode->i_fop = &v9fs_dir_operations;
  259. break;
  260. default:
  261. P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
  262. mode, mode & S_IFMT);
  263. err = -EINVAL;
  264. goto error;
  265. }
  266. return inode;
  267. error:
  268. iput(inode);
  269. return ERR_PTR(err);
  270. }
  271. /*
  272. static struct v9fs_fid*
  273. v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
  274. {
  275. int err;
  276. int nfid;
  277. struct v9fs_fid *ret;
  278. struct v9fs_fcall *fcall;
  279. nfid = v9fs_get_idpool(&v9ses->fidpool);
  280. if (nfid < 0) {
  281. eprintk(KERN_WARNING, "no free fids available\n");
  282. return ERR_PTR(-ENOSPC);
  283. }
  284. err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
  285. &fcall);
  286. if (err < 0) {
  287. if (fcall && fcall->id == RWALK)
  288. goto clunk_fid;
  289. PRINT_FCALL_ERROR("walk error", fcall);
  290. v9fs_put_idpool(nfid, &v9ses->fidpool);
  291. goto error;
  292. }
  293. kfree(fcall);
  294. fcall = NULL;
  295. ret = v9fs_fid_create(v9ses, nfid);
  296. if (!ret) {
  297. err = -ENOMEM;
  298. goto clunk_fid;
  299. }
  300. err = v9fs_fid_insert(ret, dentry);
  301. if (err < 0) {
  302. v9fs_fid_destroy(ret);
  303. goto clunk_fid;
  304. }
  305. return ret;
  306. clunk_fid:
  307. v9fs_t_clunk(v9ses, nfid);
  308. error:
  309. kfree(fcall);
  310. return ERR_PTR(err);
  311. }
  312. */
  313. /**
  314. * v9fs_clear_inode - release an inode
  315. * @inode: inode to release
  316. *
  317. */
  318. void v9fs_clear_inode(struct inode *inode)
  319. {
  320. filemap_fdatawrite(inode->i_mapping);
  321. #ifdef CONFIG_9P_FSCACHE
  322. v9fs_cache_inode_put_cookie(inode);
  323. #endif
  324. }
  325. /**
  326. * v9fs_inode_from_fid - populate an inode by issuing a attribute request
  327. * @v9ses: session information
  328. * @fid: fid to issue attribute request for
  329. * @sb: superblock on which to create inode
  330. *
  331. */
  332. static struct inode *
  333. v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  334. struct super_block *sb)
  335. {
  336. int err, umode;
  337. struct inode *ret;
  338. struct p9_wstat *st;
  339. ret = NULL;
  340. st = p9_client_stat(fid);
  341. if (IS_ERR(st))
  342. return ERR_CAST(st);
  343. umode = p9mode2unixmode(v9ses, st->mode);
  344. ret = v9fs_get_inode(sb, umode);
  345. if (IS_ERR(ret)) {
  346. err = PTR_ERR(ret);
  347. goto error;
  348. }
  349. v9fs_stat2inode(st, ret, sb);
  350. ret->i_ino = v9fs_qid2ino(&st->qid);
  351. #ifdef CONFIG_9P_FSCACHE
  352. v9fs_vcookie_set_qid(ret, &st->qid);
  353. v9fs_cache_inode_get_cookie(ret);
  354. #endif
  355. p9stat_free(st);
  356. kfree(st);
  357. return ret;
  358. error:
  359. p9stat_free(st);
  360. kfree(st);
  361. return ERR_PTR(err);
  362. }
  363. /**
  364. * v9fs_remove - helper function to remove files and directories
  365. * @dir: directory inode that is being deleted
  366. * @file: dentry that is being deleted
  367. * @rmdir: removing a directory
  368. *
  369. */
  370. static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
  371. {
  372. struct inode *file_inode;
  373. struct v9fs_session_info *v9ses;
  374. struct p9_fid *v9fid;
  375. P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
  376. rmdir);
  377. file_inode = file->d_inode;
  378. v9ses = v9fs_inode2v9ses(file_inode);
  379. v9fid = v9fs_fid_clone(file);
  380. if (IS_ERR(v9fid))
  381. return PTR_ERR(v9fid);
  382. return p9_client_remove(v9fid);
  383. }
  384. static int
  385. v9fs_open_created(struct inode *inode, struct file *file)
  386. {
  387. return 0;
  388. }
  389. /**
  390. * v9fs_create - Create a file
  391. * @v9ses: session information
  392. * @dir: directory that dentry is being created in
  393. * @dentry: dentry that is being created
  394. * @extension: 9p2000.u extension string to support devices, etc.
  395. * @perm: create permissions
  396. * @mode: open mode
  397. *
  398. */
  399. static struct p9_fid *
  400. v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
  401. struct dentry *dentry, char *extension, u32 perm, u8 mode)
  402. {
  403. int err;
  404. char *name;
  405. struct p9_fid *dfid, *ofid, *fid;
  406. struct inode *inode;
  407. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  408. err = 0;
  409. ofid = NULL;
  410. fid = NULL;
  411. name = (char *) dentry->d_name.name;
  412. dfid = v9fs_fid_clone(dentry->d_parent);
  413. if (IS_ERR(dfid)) {
  414. err = PTR_ERR(dfid);
  415. P9_DPRINTK(P9_DEBUG_VFS, "fid clone failed %d\n", err);
  416. dfid = NULL;
  417. goto error;
  418. }
  419. /* clone a fid to use for creation */
  420. ofid = p9_client_walk(dfid, 0, NULL, 1);
  421. if (IS_ERR(ofid)) {
  422. err = PTR_ERR(ofid);
  423. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  424. ofid = NULL;
  425. goto error;
  426. }
  427. err = p9_client_fcreate(ofid, name, perm, mode, extension);
  428. if (err < 0) {
  429. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
  430. goto error;
  431. }
  432. /* now walk from the parent so we can get unopened fid */
  433. fid = p9_client_walk(dfid, 1, &name, 0);
  434. if (IS_ERR(fid)) {
  435. err = PTR_ERR(fid);
  436. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  437. fid = NULL;
  438. goto error;
  439. } else
  440. dfid = NULL;
  441. /* instantiate inode and assign the unopened fid to the dentry */
  442. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  443. if (IS_ERR(inode)) {
  444. err = PTR_ERR(inode);
  445. P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
  446. goto error;
  447. }
  448. if (v9ses->cache)
  449. dentry->d_op = &v9fs_cached_dentry_operations;
  450. else
  451. dentry->d_op = &v9fs_dentry_operations;
  452. d_instantiate(dentry, inode);
  453. err = v9fs_fid_add(dentry, fid);
  454. if (err < 0)
  455. goto error;
  456. return ofid;
  457. error:
  458. if (dfid)
  459. p9_client_clunk(dfid);
  460. if (ofid)
  461. p9_client_clunk(ofid);
  462. if (fid)
  463. p9_client_clunk(fid);
  464. return ERR_PTR(err);
  465. }
  466. /**
  467. * v9fs_vfs_create - VFS hook to create files
  468. * @dir: directory inode that is being created
  469. * @dentry: dentry that is being deleted
  470. * @mode: create permissions
  471. * @nd: path information
  472. *
  473. */
  474. static int
  475. v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  476. struct nameidata *nd)
  477. {
  478. int err;
  479. u32 perm;
  480. int flags;
  481. struct v9fs_session_info *v9ses;
  482. struct p9_fid *fid;
  483. struct file *filp;
  484. err = 0;
  485. fid = NULL;
  486. v9ses = v9fs_inode2v9ses(dir);
  487. perm = unixmode2p9mode(v9ses, mode);
  488. if (nd && nd->flags & LOOKUP_OPEN)
  489. flags = nd->intent.open.flags - 1;
  490. else
  491. flags = O_RDWR;
  492. fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
  493. v9fs_uflags2omode(flags,
  494. v9fs_proto_dotu(v9ses)));
  495. if (IS_ERR(fid)) {
  496. err = PTR_ERR(fid);
  497. fid = NULL;
  498. goto error;
  499. }
  500. /* if we are opening a file, assign the open fid to the file */
  501. if (nd && nd->flags & LOOKUP_OPEN) {
  502. filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
  503. if (IS_ERR(filp)) {
  504. err = PTR_ERR(filp);
  505. goto error;
  506. }
  507. filp->private_data = fid;
  508. } else
  509. p9_client_clunk(fid);
  510. return 0;
  511. error:
  512. if (fid)
  513. p9_client_clunk(fid);
  514. return err;
  515. }
  516. /**
  517. * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
  518. * @dir: inode that is being unlinked
  519. * @dentry: dentry that is being unlinked
  520. * @mode: mode for new directory
  521. *
  522. */
  523. static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  524. {
  525. int err;
  526. u32 perm;
  527. struct v9fs_session_info *v9ses;
  528. struct p9_fid *fid;
  529. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  530. err = 0;
  531. v9ses = v9fs_inode2v9ses(dir);
  532. perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
  533. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
  534. if (IS_ERR(fid)) {
  535. err = PTR_ERR(fid);
  536. fid = NULL;
  537. }
  538. if (fid)
  539. p9_client_clunk(fid);
  540. return err;
  541. }
  542. /**
  543. * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
  544. * @dir: inode that is being walked from
  545. * @dentry: dentry that is being walked to?
  546. * @nameidata: path data
  547. *
  548. */
  549. static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
  550. struct nameidata *nameidata)
  551. {
  552. struct super_block *sb;
  553. struct v9fs_session_info *v9ses;
  554. struct p9_fid *dfid, *fid;
  555. struct inode *inode;
  556. char *name;
  557. int result = 0;
  558. P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
  559. dir, dentry->d_name.name, dentry, nameidata);
  560. sb = dir->i_sb;
  561. v9ses = v9fs_inode2v9ses(dir);
  562. dfid = v9fs_fid_lookup(dentry->d_parent);
  563. if (IS_ERR(dfid))
  564. return ERR_CAST(dfid);
  565. name = (char *) dentry->d_name.name;
  566. fid = p9_client_walk(dfid, 1, &name, 1);
  567. if (IS_ERR(fid)) {
  568. result = PTR_ERR(fid);
  569. if (result == -ENOENT) {
  570. d_add(dentry, NULL);
  571. return NULL;
  572. }
  573. return ERR_PTR(result);
  574. }
  575. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  576. if (IS_ERR(inode)) {
  577. result = PTR_ERR(inode);
  578. inode = NULL;
  579. goto error;
  580. }
  581. result = v9fs_fid_add(dentry, fid);
  582. if (result < 0)
  583. goto error;
  584. if ((fid->qid.version) && (v9ses->cache))
  585. dentry->d_op = &v9fs_cached_dentry_operations;
  586. else
  587. dentry->d_op = &v9fs_dentry_operations;
  588. d_add(dentry, inode);
  589. return NULL;
  590. error:
  591. p9_client_clunk(fid);
  592. return ERR_PTR(result);
  593. }
  594. /**
  595. * v9fs_vfs_unlink - VFS unlink hook to delete an inode
  596. * @i: inode that is being unlinked
  597. * @d: dentry that is being unlinked
  598. *
  599. */
  600. static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
  601. {
  602. return v9fs_remove(i, d, 0);
  603. }
  604. /**
  605. * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
  606. * @i: inode that is being unlinked
  607. * @d: dentry that is being unlinked
  608. *
  609. */
  610. static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
  611. {
  612. return v9fs_remove(i, d, 1);
  613. }
  614. /**
  615. * v9fs_vfs_rename - VFS hook to rename an inode
  616. * @old_dir: old dir inode
  617. * @old_dentry: old dentry
  618. * @new_dir: new dir inode
  619. * @new_dentry: new dentry
  620. *
  621. */
  622. static int
  623. v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  624. struct inode *new_dir, struct dentry *new_dentry)
  625. {
  626. struct inode *old_inode;
  627. struct v9fs_session_info *v9ses;
  628. struct p9_fid *oldfid;
  629. struct p9_fid *olddirfid;
  630. struct p9_fid *newdirfid;
  631. struct p9_wstat wstat;
  632. int retval;
  633. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  634. retval = 0;
  635. old_inode = old_dentry->d_inode;
  636. v9ses = v9fs_inode2v9ses(old_inode);
  637. oldfid = v9fs_fid_lookup(old_dentry);
  638. if (IS_ERR(oldfid))
  639. return PTR_ERR(oldfid);
  640. olddirfid = v9fs_fid_clone(old_dentry->d_parent);
  641. if (IS_ERR(olddirfid)) {
  642. retval = PTR_ERR(olddirfid);
  643. goto done;
  644. }
  645. newdirfid = v9fs_fid_clone(new_dentry->d_parent);
  646. if (IS_ERR(newdirfid)) {
  647. retval = PTR_ERR(newdirfid);
  648. goto clunk_olddir;
  649. }
  650. /* 9P can only handle file rename in the same directory */
  651. if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
  652. P9_DPRINTK(P9_DEBUG_ERROR,
  653. "old dir and new dir are different\n");
  654. retval = -EXDEV;
  655. goto clunk_newdir;
  656. }
  657. v9fs_blank_wstat(&wstat);
  658. wstat.muid = v9ses->uname;
  659. wstat.name = (char *) new_dentry->d_name.name;
  660. retval = p9_client_wstat(oldfid, &wstat);
  661. clunk_newdir:
  662. p9_client_clunk(newdirfid);
  663. clunk_olddir:
  664. p9_client_clunk(olddirfid);
  665. done:
  666. return retval;
  667. }
  668. /**
  669. * v9fs_vfs_getattr - retrieve file metadata
  670. * @mnt: mount information
  671. * @dentry: file to get attributes on
  672. * @stat: metadata structure to populate
  673. *
  674. */
  675. static int
  676. v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  677. struct kstat *stat)
  678. {
  679. int err;
  680. struct v9fs_session_info *v9ses;
  681. struct p9_fid *fid;
  682. struct p9_wstat *st;
  683. P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  684. err = -EPERM;
  685. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  686. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  687. return simple_getattr(mnt, dentry, stat);
  688. fid = v9fs_fid_lookup(dentry);
  689. if (IS_ERR(fid))
  690. return PTR_ERR(fid);
  691. st = p9_client_stat(fid);
  692. if (IS_ERR(st))
  693. return PTR_ERR(st);
  694. v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
  695. generic_fillattr(dentry->d_inode, stat);
  696. kfree(st);
  697. return 0;
  698. }
  699. /**
  700. * v9fs_vfs_setattr - set file metadata
  701. * @dentry: file whose metadata to set
  702. * @iattr: metadata assignment structure
  703. *
  704. */
  705. static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
  706. {
  707. int retval;
  708. struct v9fs_session_info *v9ses;
  709. struct p9_fid *fid;
  710. struct p9_wstat wstat;
  711. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  712. retval = -EPERM;
  713. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  714. fid = v9fs_fid_lookup(dentry);
  715. if(IS_ERR(fid))
  716. return PTR_ERR(fid);
  717. v9fs_blank_wstat(&wstat);
  718. if (iattr->ia_valid & ATTR_MODE)
  719. wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
  720. if (iattr->ia_valid & ATTR_MTIME)
  721. wstat.mtime = iattr->ia_mtime.tv_sec;
  722. if (iattr->ia_valid & ATTR_ATIME)
  723. wstat.atime = iattr->ia_atime.tv_sec;
  724. if (iattr->ia_valid & ATTR_SIZE)
  725. wstat.length = iattr->ia_size;
  726. if (v9fs_proto_dotu(v9ses)) {
  727. if (iattr->ia_valid & ATTR_UID)
  728. wstat.n_uid = iattr->ia_uid;
  729. if (iattr->ia_valid & ATTR_GID)
  730. wstat.n_gid = iattr->ia_gid;
  731. }
  732. retval = p9_client_wstat(fid, &wstat);
  733. if (retval >= 0)
  734. retval = inode_setattr(dentry->d_inode, iattr);
  735. return retval;
  736. }
  737. /**
  738. * v9fs_stat2inode - populate an inode structure with mistat info
  739. * @stat: Plan 9 metadata (mistat) structure
  740. * @inode: inode to populate
  741. * @sb: superblock of filesystem
  742. *
  743. */
  744. void
  745. v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
  746. struct super_block *sb)
  747. {
  748. char ext[32];
  749. char tag_name[14];
  750. unsigned int i_nlink;
  751. struct v9fs_session_info *v9ses = sb->s_fs_info;
  752. inode->i_nlink = 1;
  753. inode->i_atime.tv_sec = stat->atime;
  754. inode->i_mtime.tv_sec = stat->mtime;
  755. inode->i_ctime.tv_sec = stat->mtime;
  756. inode->i_uid = v9ses->dfltuid;
  757. inode->i_gid = v9ses->dfltgid;
  758. if (v9fs_proto_dotu(v9ses)) {
  759. inode->i_uid = stat->n_uid;
  760. inode->i_gid = stat->n_gid;
  761. }
  762. if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
  763. if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
  764. /*
  765. * Hadlink support got added later to
  766. * to the .u extension. So there can be
  767. * server out there that doesn't support
  768. * this even with .u extension. So check
  769. * for non NULL stat->extension
  770. */
  771. strncpy(ext, stat->extension, sizeof(ext));
  772. /* HARDLINKCOUNT %u */
  773. sscanf(ext, "%13s %u", tag_name, &i_nlink);
  774. if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
  775. inode->i_nlink = i_nlink;
  776. }
  777. }
  778. inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
  779. if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
  780. char type = 0;
  781. int major = -1;
  782. int minor = -1;
  783. strncpy(ext, stat->extension, sizeof(ext));
  784. sscanf(ext, "%c %u %u", &type, &major, &minor);
  785. switch (type) {
  786. case 'c':
  787. inode->i_mode &= ~S_IFBLK;
  788. inode->i_mode |= S_IFCHR;
  789. break;
  790. case 'b':
  791. break;
  792. default:
  793. P9_DPRINTK(P9_DEBUG_ERROR,
  794. "Unknown special type %c %s\n", type,
  795. stat->extension);
  796. };
  797. inode->i_rdev = MKDEV(major, minor);
  798. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  799. } else
  800. inode->i_rdev = 0;
  801. i_size_write(inode, stat->length);
  802. /* not real number of blocks, but 512 byte ones ... */
  803. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  804. }
  805. /**
  806. * v9fs_qid2ino - convert qid into inode number
  807. * @qid: qid to hash
  808. *
  809. * BUG: potential for inode number collisions?
  810. */
  811. ino_t v9fs_qid2ino(struct p9_qid *qid)
  812. {
  813. u64 path = qid->path + 2;
  814. ino_t i = 0;
  815. if (sizeof(ino_t) == sizeof(path))
  816. memcpy(&i, &path, sizeof(ino_t));
  817. else
  818. i = (ino_t) (path ^ (path >> 32));
  819. return i;
  820. }
  821. /**
  822. * v9fs_readlink - read a symlink's location (internal version)
  823. * @dentry: dentry for symlink
  824. * @buffer: buffer to load symlink location into
  825. * @buflen: length of buffer
  826. *
  827. */
  828. static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
  829. {
  830. int retval;
  831. struct v9fs_session_info *v9ses;
  832. struct p9_fid *fid;
  833. struct p9_wstat *st;
  834. P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
  835. retval = -EPERM;
  836. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  837. fid = v9fs_fid_lookup(dentry);
  838. if (IS_ERR(fid))
  839. return PTR_ERR(fid);
  840. if (!v9fs_proto_dotu(v9ses))
  841. return -EBADF;
  842. st = p9_client_stat(fid);
  843. if (IS_ERR(st))
  844. return PTR_ERR(st);
  845. if (!(st->mode & P9_DMSYMLINK)) {
  846. retval = -EINVAL;
  847. goto done;
  848. }
  849. /* copy extension buffer into buffer */
  850. strncpy(buffer, st->extension, buflen);
  851. P9_DPRINTK(P9_DEBUG_VFS,
  852. "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
  853. retval = strnlen(buffer, buflen);
  854. done:
  855. kfree(st);
  856. return retval;
  857. }
  858. /**
  859. * v9fs_vfs_follow_link - follow a symlink path
  860. * @dentry: dentry for symlink
  861. * @nd: nameidata
  862. *
  863. */
  864. static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  865. {
  866. int len = 0;
  867. char *link = __getname();
  868. P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
  869. if (!link)
  870. link = ERR_PTR(-ENOMEM);
  871. else {
  872. len = v9fs_readlink(dentry, link, PATH_MAX);
  873. if (len < 0) {
  874. __putname(link);
  875. link = ERR_PTR(len);
  876. } else
  877. link[min(len, PATH_MAX-1)] = 0;
  878. }
  879. nd_set_link(nd, link);
  880. return NULL;
  881. }
  882. /**
  883. * v9fs_vfs_put_link - release a symlink path
  884. * @dentry: dentry for symlink
  885. * @nd: nameidata
  886. * @p: unused
  887. *
  888. */
  889. static void
  890. v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  891. {
  892. char *s = nd_get_link(nd);
  893. P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
  894. IS_ERR(s) ? "<error>" : s);
  895. if (!IS_ERR(s))
  896. __putname(s);
  897. }
  898. /**
  899. * v9fs_vfs_mkspecial - create a special file
  900. * @dir: inode to create special file in
  901. * @dentry: dentry to create
  902. * @mode: mode to create special file
  903. * @extension: 9p2000.u format extension string representing special file
  904. *
  905. */
  906. static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
  907. int mode, const char *extension)
  908. {
  909. u32 perm;
  910. struct v9fs_session_info *v9ses;
  911. struct p9_fid *fid;
  912. v9ses = v9fs_inode2v9ses(dir);
  913. if (!v9fs_proto_dotu(v9ses)) {
  914. P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
  915. return -EPERM;
  916. }
  917. perm = unixmode2p9mode(v9ses, mode);
  918. fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
  919. P9_OREAD);
  920. if (IS_ERR(fid))
  921. return PTR_ERR(fid);
  922. p9_client_clunk(fid);
  923. return 0;
  924. }
  925. /**
  926. * v9fs_vfs_symlink - helper function to create symlinks
  927. * @dir: directory inode containing symlink
  928. * @dentry: dentry for symlink
  929. * @symname: symlink data
  930. *
  931. * See Also: 9P2000.u RFC for more information
  932. *
  933. */
  934. static int
  935. v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  936. {
  937. P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
  938. dentry->d_name.name, symname);
  939. return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
  940. }
  941. /**
  942. * v9fs_vfs_link - create a hardlink
  943. * @old_dentry: dentry for file to link to
  944. * @dir: inode destination for new link
  945. * @dentry: dentry for link
  946. *
  947. */
  948. static int
  949. v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
  950. struct dentry *dentry)
  951. {
  952. int retval;
  953. struct p9_fid *oldfid;
  954. char *name;
  955. P9_DPRINTK(P9_DEBUG_VFS,
  956. " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
  957. old_dentry->d_name.name);
  958. oldfid = v9fs_fid_clone(old_dentry);
  959. if (IS_ERR(oldfid))
  960. return PTR_ERR(oldfid);
  961. name = __getname();
  962. if (unlikely(!name)) {
  963. retval = -ENOMEM;
  964. goto clunk_fid;
  965. }
  966. sprintf(name, "%d\n", oldfid->fid);
  967. retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
  968. __putname(name);
  969. clunk_fid:
  970. p9_client_clunk(oldfid);
  971. return retval;
  972. }
  973. /**
  974. * v9fs_vfs_mknod - create a special file
  975. * @dir: inode destination for new link
  976. * @dentry: dentry for file
  977. * @mode: mode for creation
  978. * @rdev: device associated with special file
  979. *
  980. */
  981. static int
  982. v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  983. {
  984. int retval;
  985. char *name;
  986. P9_DPRINTK(P9_DEBUG_VFS,
  987. " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
  988. dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
  989. if (!new_valid_dev(rdev))
  990. return -EINVAL;
  991. name = __getname();
  992. if (!name)
  993. return -ENOMEM;
  994. /* build extension */
  995. if (S_ISBLK(mode))
  996. sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
  997. else if (S_ISCHR(mode))
  998. sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
  999. else if (S_ISFIFO(mode))
  1000. *name = 0;
  1001. else {
  1002. __putname(name);
  1003. return -EINVAL;
  1004. }
  1005. retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
  1006. __putname(name);
  1007. return retval;
  1008. }
  1009. static const struct inode_operations v9fs_dir_inode_operations_ext = {
  1010. .create = v9fs_vfs_create,
  1011. .lookup = v9fs_vfs_lookup,
  1012. .symlink = v9fs_vfs_symlink,
  1013. .link = v9fs_vfs_link,
  1014. .unlink = v9fs_vfs_unlink,
  1015. .mkdir = v9fs_vfs_mkdir,
  1016. .rmdir = v9fs_vfs_rmdir,
  1017. .mknod = v9fs_vfs_mknod,
  1018. .rename = v9fs_vfs_rename,
  1019. .getattr = v9fs_vfs_getattr,
  1020. .setattr = v9fs_vfs_setattr,
  1021. };
  1022. static const struct inode_operations v9fs_dir_inode_operations = {
  1023. .create = v9fs_vfs_create,
  1024. .lookup = v9fs_vfs_lookup,
  1025. .unlink = v9fs_vfs_unlink,
  1026. .mkdir = v9fs_vfs_mkdir,
  1027. .rmdir = v9fs_vfs_rmdir,
  1028. .mknod = v9fs_vfs_mknod,
  1029. .rename = v9fs_vfs_rename,
  1030. .getattr = v9fs_vfs_getattr,
  1031. .setattr = v9fs_vfs_setattr,
  1032. };
  1033. static const struct inode_operations v9fs_file_inode_operations = {
  1034. .getattr = v9fs_vfs_getattr,
  1035. .setattr = v9fs_vfs_setattr,
  1036. };
  1037. static const struct inode_operations v9fs_symlink_inode_operations = {
  1038. .readlink = generic_readlink,
  1039. .follow_link = v9fs_vfs_follow_link,
  1040. .put_link = v9fs_vfs_put_link,
  1041. .getattr = v9fs_vfs_getattr,
  1042. .setattr = v9fs_vfs_setattr,
  1043. };