vfs_inode.c 28 KB

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