vfs_inode.c 28 KB

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