vfs_inode.c 25 KB

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