vfs_inode.c 25 KB

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