vfs_inode.c 26 KB

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