vfs_inode.c 26 KB

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