vfs_inode.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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 - retreive 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. char ext[32];
  655. struct v9fs_session_info *v9ses = sb->s_fs_info;
  656. inode->i_nlink = 1;
  657. inode->i_atime.tv_sec = stat->atime;
  658. inode->i_mtime.tv_sec = stat->mtime;
  659. inode->i_ctime.tv_sec = stat->mtime;
  660. inode->i_uid = v9ses->uid;
  661. inode->i_gid = v9ses->gid;
  662. if (v9ses->extended) {
  663. inode->i_uid = stat->n_uid;
  664. inode->i_gid = stat->n_gid;
  665. }
  666. inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
  667. if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
  668. char type = 0;
  669. int major = -1;
  670. int minor = -1;
  671. v9fs_str_copy(ext, sizeof(ext), &stat->extension);
  672. sscanf(ext, "%c %u %u", &type, &major, &minor);
  673. switch (type) {
  674. case 'c':
  675. inode->i_mode &= ~S_IFBLK;
  676. inode->i_mode |= S_IFCHR;
  677. break;
  678. case 'b':
  679. break;
  680. default:
  681. dprintk(DEBUG_ERROR, "Unknown special type %c (%.*s)\n",
  682. type, stat->extension.len, stat->extension.str);
  683. };
  684. inode->i_rdev = MKDEV(major, minor);
  685. } else
  686. inode->i_rdev = 0;
  687. inode->i_size = stat->length;
  688. inode->i_blksize = sb->s_blocksize;
  689. inode->i_blocks =
  690. (inode->i_size + inode->i_blksize - 1) >> sb->s_blocksize_bits;
  691. }
  692. /**
  693. * v9fs_qid2ino - convert qid into inode number
  694. * @qid: qid to hash
  695. *
  696. * BUG: potential for inode number collisions?
  697. */
  698. ino_t v9fs_qid2ino(struct v9fs_qid *qid)
  699. {
  700. u64 path = qid->path + 2;
  701. ino_t i = 0;
  702. if (sizeof(ino_t) == sizeof(path))
  703. memcpy(&i, &path, sizeof(ino_t));
  704. else
  705. i = (ino_t) (path ^ (path >> 32));
  706. return i;
  707. }
  708. /**
  709. * v9fs_readlink - read a symlink's location (internal version)
  710. * @dentry: dentry for symlink
  711. * @buffer: buffer to load symlink location into
  712. * @buflen: length of buffer
  713. *
  714. */
  715. static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
  716. {
  717. int retval = -EPERM;
  718. struct v9fs_fcall *fcall = NULL;
  719. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
  720. struct v9fs_fid *fid = v9fs_fid_lookup(dentry);
  721. if (!fid) {
  722. dprintk(DEBUG_ERROR, "could not resolve fid from dentry\n");
  723. retval = -EBADF;
  724. goto FreeFcall;
  725. }
  726. if (!v9ses->extended) {
  727. retval = -EBADF;
  728. dprintk(DEBUG_ERROR, "not extended\n");
  729. goto FreeFcall;
  730. }
  731. dprintk(DEBUG_VFS, " %s\n", dentry->d_name.name);
  732. retval = v9fs_t_stat(v9ses, fid->fid, &fcall);
  733. if (retval < 0) {
  734. dprintk(DEBUG_ERROR, "stat error\n");
  735. goto FreeFcall;
  736. }
  737. if (!fcall)
  738. return -EIO;
  739. if (!(fcall->params.rstat.stat.mode & V9FS_DMSYMLINK)) {
  740. retval = -EINVAL;
  741. goto FreeFcall;
  742. }
  743. /* copy extension buffer into buffer */
  744. if (fcall->params.rstat.stat.extension.len < buflen)
  745. buflen = fcall->params.rstat.stat.extension.len;
  746. memcpy(buffer, fcall->params.rstat.stat.extension.str, buflen - 1);
  747. buffer[buflen-1] = 0;
  748. retval = buflen;
  749. FreeFcall:
  750. kfree(fcall);
  751. return retval;
  752. }
  753. /**
  754. * v9fs_vfs_readlink - read a symlink's location
  755. * @dentry: dentry for symlink
  756. * @buf: buffer to load symlink location into
  757. * @buflen: length of buffer
  758. *
  759. */
  760. static int v9fs_vfs_readlink(struct dentry *dentry, char __user * buffer,
  761. int buflen)
  762. {
  763. int retval;
  764. int ret;
  765. char *link = __getname();
  766. if (buflen > PATH_MAX)
  767. buflen = PATH_MAX;
  768. dprintk(DEBUG_VFS, " dentry: %s (%p)\n", dentry->d_iname, dentry);
  769. retval = v9fs_readlink(dentry, link, buflen);
  770. if (retval > 0) {
  771. if ((ret = copy_to_user(buffer, link, retval)) != 0) {
  772. dprintk(DEBUG_ERROR, "problem copying to user: %d\n",
  773. ret);
  774. retval = ret;
  775. }
  776. }
  777. __putname(link);
  778. return retval;
  779. }
  780. /**
  781. * v9fs_vfs_follow_link - follow a symlink path
  782. * @dentry: dentry for symlink
  783. * @nd: nameidata
  784. *
  785. */
  786. static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  787. {
  788. int len = 0;
  789. char *link = __getname();
  790. dprintk(DEBUG_VFS, "%s n", dentry->d_name.name);
  791. if (!link)
  792. link = ERR_PTR(-ENOMEM);
  793. else {
  794. len = v9fs_readlink(dentry, link, strlen(link));
  795. if (len < 0) {
  796. __putname(link);
  797. link = ERR_PTR(len);
  798. } else
  799. link[len] = 0;
  800. }
  801. nd_set_link(nd, link);
  802. return NULL;
  803. }
  804. /**
  805. * v9fs_vfs_put_link - release a symlink path
  806. * @dentry: dentry for symlink
  807. * @nd: nameidata
  808. *
  809. */
  810. static void v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  811. {
  812. char *s = nd_get_link(nd);
  813. dprintk(DEBUG_VFS, " %s %s\n", dentry->d_name.name, s);
  814. if (!IS_ERR(s))
  815. __putname(s);
  816. }
  817. static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
  818. int mode, const char *extension)
  819. {
  820. int err, retval;
  821. struct v9fs_session_info *v9ses;
  822. struct v9fs_fcall *fcall;
  823. struct v9fs_fid *fid;
  824. struct v9fs_wstat wstat;
  825. v9ses = v9fs_inode2v9ses(dir);
  826. retval = -EPERM;
  827. fcall = NULL;
  828. if (!v9ses->extended) {
  829. dprintk(DEBUG_ERROR, "not extended\n");
  830. goto free_mem;
  831. }
  832. /* issue a create */
  833. retval = v9fs_create(dir, dentry, mode, 0);
  834. if (retval != 0)
  835. goto free_mem;
  836. fid = v9fs_fid_get_created(dentry);
  837. if (!fid) {
  838. dprintk(DEBUG_ERROR, "couldn't resolve fid from dentry\n");
  839. goto free_mem;
  840. }
  841. /* issue a Twstat */
  842. v9fs_blank_wstat(&wstat);
  843. wstat.muid = v9ses->name;
  844. wstat.extension = (char *) extension;
  845. retval = v9fs_t_wstat(v9ses, fid->fid, &wstat, &fcall);
  846. if (retval < 0) {
  847. PRINT_FCALL_ERROR("wstat error", fcall);
  848. goto free_mem;
  849. }
  850. err = v9fs_t_clunk(v9ses, fid->fid);
  851. if (err < 0) {
  852. dprintk(DEBUG_ERROR, "clunk failed: %d\n", err);
  853. goto free_mem;
  854. }
  855. d_drop(dentry); /* FID - will this also clunk? */
  856. free_mem:
  857. kfree(fcall);
  858. return retval;
  859. }
  860. /**
  861. * v9fs_vfs_symlink - helper function to create symlinks
  862. * @dir: directory inode containing symlink
  863. * @dentry: dentry for symlink
  864. * @symname: symlink data
  865. *
  866. * See 9P2000.u RFC for more information
  867. *
  868. */
  869. static int
  870. v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  871. {
  872. dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
  873. symname);
  874. return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
  875. }
  876. /**
  877. * v9fs_vfs_link - create a hardlink
  878. * @old_dentry: dentry for file to link to
  879. * @dir: inode destination for new link
  880. * @dentry: dentry for link
  881. *
  882. */
  883. /* XXX - lots of code dup'd from symlink and creates,
  884. * figure out a better reuse strategy
  885. */
  886. static int
  887. v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
  888. struct dentry *dentry)
  889. {
  890. int retval;
  891. struct v9fs_fid *oldfid;
  892. char *name;
  893. dprintk(DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
  894. old_dentry->d_name.name);
  895. oldfid = v9fs_fid_lookup(old_dentry);
  896. if (!oldfid) {
  897. dprintk(DEBUG_ERROR, "can't find oldfid\n");
  898. return -EPERM;
  899. }
  900. name = __getname();
  901. sprintf(name, "hardlink(%d)\n", oldfid->fid);
  902. retval = v9fs_vfs_mkspecial(dir, dentry, V9FS_DMLINK, name);
  903. __putname(name);
  904. return retval;
  905. }
  906. /**
  907. * v9fs_vfs_mknod - create a special file
  908. * @dir: inode destination for new link
  909. * @dentry: dentry for file
  910. * @mode: mode for creation
  911. * @dev_t: device associated with special file
  912. *
  913. */
  914. static int
  915. v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  916. {
  917. int retval;
  918. char *name;
  919. dprintk(DEBUG_VFS, " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
  920. dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
  921. if (!new_valid_dev(rdev))
  922. return -EINVAL;
  923. name = __getname();
  924. /* build extension */
  925. if (S_ISBLK(mode))
  926. sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
  927. else if (S_ISCHR(mode))
  928. sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
  929. else if (S_ISFIFO(mode))
  930. *name = 0;
  931. else {
  932. __putname(name);
  933. return -EINVAL;
  934. }
  935. retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
  936. __putname(name);
  937. return retval;
  938. }
  939. static struct inode_operations v9fs_dir_inode_operations_ext = {
  940. .create = v9fs_vfs_create,
  941. .lookup = v9fs_vfs_lookup,
  942. .symlink = v9fs_vfs_symlink,
  943. .link = v9fs_vfs_link,
  944. .unlink = v9fs_vfs_unlink,
  945. .mkdir = v9fs_vfs_mkdir,
  946. .rmdir = v9fs_vfs_rmdir,
  947. .mknod = v9fs_vfs_mknod,
  948. .rename = v9fs_vfs_rename,
  949. .readlink = v9fs_vfs_readlink,
  950. .getattr = v9fs_vfs_getattr,
  951. .setattr = v9fs_vfs_setattr,
  952. };
  953. static struct inode_operations v9fs_dir_inode_operations = {
  954. .create = v9fs_vfs_create,
  955. .lookup = v9fs_vfs_lookup,
  956. .unlink = v9fs_vfs_unlink,
  957. .mkdir = v9fs_vfs_mkdir,
  958. .rmdir = v9fs_vfs_rmdir,
  959. .mknod = v9fs_vfs_mknod,
  960. .rename = v9fs_vfs_rename,
  961. .getattr = v9fs_vfs_getattr,
  962. .setattr = v9fs_vfs_setattr,
  963. };
  964. static struct inode_operations v9fs_file_inode_operations = {
  965. .getattr = v9fs_vfs_getattr,
  966. .setattr = v9fs_vfs_setattr,
  967. };
  968. static struct inode_operations v9fs_symlink_inode_operations = {
  969. .readlink = v9fs_vfs_readlink,
  970. .follow_link = v9fs_vfs_follow_link,
  971. .put_link = v9fs_vfs_put_link,
  972. .getattr = v9fs_vfs_getattr,
  973. .setattr = v9fs_vfs_setattr,
  974. };