vfs_inode.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  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. dirfid = v9fs_fid_lookup(dentry->d_parent);
  524. if (!dirfid) {
  525. dprintk(DEBUG_ERROR, "no dirfid\n");
  526. return ERR_PTR(-EINVAL);
  527. }
  528. dirfidnum = dirfid->fid;
  529. if (dirfidnum < 0) {
  530. dprintk(DEBUG_ERROR, "no dirfid for inode %p, #%lu\n",
  531. dir, dir->i_ino);
  532. return ERR_PTR(-EBADF);
  533. }
  534. newfid = v9fs_get_idpool(&v9ses->fidpool);
  535. if (newfid < 0) {
  536. eprintk(KERN_WARNING, "newfid fails!\n");
  537. return ERR_PTR(-ENOSPC);
  538. }
  539. result = v9fs_t_walk(v9ses, dirfidnum, newfid,
  540. (char *)dentry->d_name.name, NULL);
  541. if (result < 0) {
  542. v9fs_put_idpool(newfid, &v9ses->fidpool);
  543. if (result == -ENOENT) {
  544. d_add(dentry, NULL);
  545. dprintk(DEBUG_VFS,
  546. "Return negative dentry %p count %d\n",
  547. dentry, atomic_read(&dentry->d_count));
  548. return NULL;
  549. }
  550. dprintk(DEBUG_ERROR, "walk error:%d\n", result);
  551. goto FreeFcall;
  552. }
  553. result = v9fs_t_stat(v9ses, newfid, &fcall);
  554. if (result < 0) {
  555. dprintk(DEBUG_ERROR, "stat error\n");
  556. goto FreeFcall;
  557. }
  558. inode = v9fs_get_inode(sb, p9mode2unixmode(v9ses,
  559. fcall->params.rstat.stat.mode));
  560. if (IS_ERR(inode) && (PTR_ERR(inode) == -ENOSPC)) {
  561. eprintk(KERN_WARNING, "inode alloc failes, returns %ld\n",
  562. PTR_ERR(inode));
  563. result = -ENOSPC;
  564. goto FreeFcall;
  565. }
  566. inode->i_ino = v9fs_qid2ino(&fcall->params.rstat.stat.qid);
  567. fid = v9fs_fid_create(v9ses, newfid);
  568. if (fid == NULL) {
  569. dprintk(DEBUG_ERROR, "couldn't insert\n");
  570. result = -ENOMEM;
  571. goto FreeFcall;
  572. }
  573. result = v9fs_fid_insert(fid, dentry);
  574. if (result < 0)
  575. goto FreeFcall;
  576. fid->qid = fcall->params.rstat.stat.qid;
  577. dentry->d_op = &v9fs_dentry_operations;
  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. };