vfs_inode.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /*
  2. * linux/fs/9p/vfs_inode.c
  3. *
  4. * This file contains vfs inode ops for the 9P2000 protocol.
  5. *
  6. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  7. * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to:
  20. * Free Software Foundation
  21. * 51 Franklin Street, Fifth Floor
  22. * Boston, MA 02111-1301 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/errno.h>
  27. #include <linux/fs.h>
  28. #include <linux/file.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/inet.h>
  33. #include <linux/namei.h>
  34. #include <linux/idr.h>
  35. #include <linux/sched.h>
  36. #include <linux/slab.h>
  37. #include <net/9p/9p.h>
  38. #include <net/9p/client.h>
  39. #include "v9fs.h"
  40. #include "v9fs_vfs.h"
  41. #include "fid.h"
  42. #include "cache.h"
  43. static const struct inode_operations v9fs_dir_inode_operations;
  44. static const struct inode_operations v9fs_dir_inode_operations_dotu;
  45. static const struct inode_operations v9fs_dir_inode_operations_dotl;
  46. static const struct inode_operations v9fs_file_inode_operations;
  47. static const struct inode_operations v9fs_file_inode_operations_dotl;
  48. static const struct inode_operations v9fs_symlink_inode_operations;
  49. static const struct inode_operations v9fs_symlink_inode_operations_dotl;
  50. /**
  51. * unixmode2p9mode - convert unix mode bits to plan 9
  52. * @v9ses: v9fs session information
  53. * @mode: mode to convert
  54. *
  55. */
  56. static int unixmode2p9mode(struct v9fs_session_info *v9ses, int mode)
  57. {
  58. int res;
  59. res = mode & 0777;
  60. if (S_ISDIR(mode))
  61. res |= P9_DMDIR;
  62. if (v9fs_proto_dotu(v9ses)) {
  63. if (S_ISLNK(mode))
  64. res |= P9_DMSYMLINK;
  65. if (v9ses->nodev == 0) {
  66. if (S_ISSOCK(mode))
  67. res |= P9_DMSOCKET;
  68. if (S_ISFIFO(mode))
  69. res |= P9_DMNAMEDPIPE;
  70. if (S_ISBLK(mode))
  71. res |= P9_DMDEVICE;
  72. if (S_ISCHR(mode))
  73. res |= P9_DMDEVICE;
  74. }
  75. if ((mode & S_ISUID) == S_ISUID)
  76. res |= P9_DMSETUID;
  77. if ((mode & S_ISGID) == S_ISGID)
  78. res |= P9_DMSETGID;
  79. if ((mode & S_ISVTX) == S_ISVTX)
  80. res |= P9_DMSETVTX;
  81. if ((mode & P9_DMLINK))
  82. res |= P9_DMLINK;
  83. }
  84. return res;
  85. }
  86. /**
  87. * p9mode2unixmode- convert plan9 mode bits to unix mode bits
  88. * @v9ses: v9fs session information
  89. * @mode: mode to convert
  90. *
  91. */
  92. static int p9mode2unixmode(struct v9fs_session_info *v9ses, int mode)
  93. {
  94. int res;
  95. res = mode & 0777;
  96. if ((mode & P9_DMDIR) == P9_DMDIR)
  97. res |= S_IFDIR;
  98. else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
  99. res |= S_IFLNK;
  100. else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
  101. && (v9ses->nodev == 0))
  102. res |= S_IFSOCK;
  103. else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
  104. && (v9ses->nodev == 0))
  105. res |= S_IFIFO;
  106. else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
  107. && (v9ses->nodev == 0))
  108. res |= S_IFBLK;
  109. else
  110. res |= S_IFREG;
  111. if (v9fs_proto_dotu(v9ses)) {
  112. if ((mode & P9_DMSETUID) == P9_DMSETUID)
  113. res |= S_ISUID;
  114. if ((mode & P9_DMSETGID) == P9_DMSETGID)
  115. res |= S_ISGID;
  116. if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
  117. res |= S_ISVTX;
  118. }
  119. return res;
  120. }
  121. /**
  122. * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
  123. * @uflags: flags to convert
  124. * @extended: if .u extensions are active
  125. */
  126. int v9fs_uflags2omode(int uflags, int extended)
  127. {
  128. int ret;
  129. ret = 0;
  130. switch (uflags&3) {
  131. default:
  132. case O_RDONLY:
  133. ret = P9_OREAD;
  134. break;
  135. case O_WRONLY:
  136. ret = P9_OWRITE;
  137. break;
  138. case O_RDWR:
  139. ret = P9_ORDWR;
  140. break;
  141. }
  142. if (uflags & O_TRUNC)
  143. ret |= P9_OTRUNC;
  144. if (extended) {
  145. if (uflags & O_EXCL)
  146. ret |= P9_OEXCL;
  147. if (uflags & O_APPEND)
  148. ret |= P9_OAPPEND;
  149. }
  150. return ret;
  151. }
  152. /**
  153. * v9fs_blank_wstat - helper function to setup a 9P stat structure
  154. * @wstat: structure to initialize
  155. *
  156. */
  157. void
  158. v9fs_blank_wstat(struct p9_wstat *wstat)
  159. {
  160. wstat->type = ~0;
  161. wstat->dev = ~0;
  162. wstat->qid.type = ~0;
  163. wstat->qid.version = ~0;
  164. *((long long *)&wstat->qid.path) = ~0;
  165. wstat->mode = ~0;
  166. wstat->atime = ~0;
  167. wstat->mtime = ~0;
  168. wstat->length = ~0;
  169. wstat->name = NULL;
  170. wstat->uid = NULL;
  171. wstat->gid = NULL;
  172. wstat->muid = NULL;
  173. wstat->n_uid = ~0;
  174. wstat->n_gid = ~0;
  175. wstat->n_muid = ~0;
  176. wstat->extension = NULL;
  177. }
  178. #ifdef CONFIG_9P_FSCACHE
  179. /**
  180. * v9fs_alloc_inode - helper function to allocate an inode
  181. * This callback is executed before setting up the inode so that we
  182. * can associate a vcookie with each inode.
  183. *
  184. */
  185. struct inode *v9fs_alloc_inode(struct super_block *sb)
  186. {
  187. struct v9fs_cookie *vcookie;
  188. vcookie = (struct v9fs_cookie *)kmem_cache_alloc(vcookie_cache,
  189. GFP_KERNEL);
  190. if (!vcookie)
  191. return NULL;
  192. vcookie->fscache = NULL;
  193. vcookie->qid = NULL;
  194. spin_lock_init(&vcookie->lock);
  195. return &vcookie->inode;
  196. }
  197. /**
  198. * v9fs_destroy_inode - destroy an inode
  199. *
  200. */
  201. void v9fs_destroy_inode(struct inode *inode)
  202. {
  203. kmem_cache_free(vcookie_cache, v9fs_inode2cookie(inode));
  204. }
  205. #endif
  206. /**
  207. * v9fs_get_inode - helper function to setup an inode
  208. * @sb: superblock
  209. * @mode: mode to setup inode with
  210. *
  211. */
  212. struct inode *v9fs_get_inode(struct super_block *sb, int mode)
  213. {
  214. int err;
  215. struct inode *inode;
  216. struct v9fs_session_info *v9ses = sb->s_fs_info;
  217. P9_DPRINTK(P9_DEBUG_VFS, "super block: %p mode: %o\n", sb, mode);
  218. inode = new_inode(sb);
  219. if (!inode) {
  220. P9_EPRINTK(KERN_WARNING, "Problem allocating inode\n");
  221. return ERR_PTR(-ENOMEM);
  222. }
  223. inode->i_mode = mode;
  224. inode->i_uid = current_fsuid();
  225. inode->i_gid = current_fsgid();
  226. inode->i_blocks = 0;
  227. inode->i_rdev = 0;
  228. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  229. inode->i_mapping->a_ops = &v9fs_addr_operations;
  230. switch (mode & S_IFMT) {
  231. case S_IFIFO:
  232. case S_IFBLK:
  233. case S_IFCHR:
  234. case S_IFSOCK:
  235. if (!v9fs_proto_dotu(v9ses)) {
  236. P9_DPRINTK(P9_DEBUG_ERROR,
  237. "special files without extended mode\n");
  238. err = -EINVAL;
  239. goto error;
  240. }
  241. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  242. break;
  243. case S_IFREG:
  244. if (v9fs_proto_dotl(v9ses)) {
  245. inode->i_op = &v9fs_file_inode_operations_dotl;
  246. inode->i_fop = &v9fs_file_operations_dotl;
  247. } else {
  248. inode->i_op = &v9fs_file_inode_operations;
  249. inode->i_fop = &v9fs_file_operations;
  250. }
  251. break;
  252. case S_IFLNK:
  253. if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
  254. P9_DPRINTK(P9_DEBUG_ERROR, "extended modes used with "
  255. "legacy protocol.\n");
  256. err = -EINVAL;
  257. goto error;
  258. }
  259. if (v9fs_proto_dotl(v9ses))
  260. inode->i_op = &v9fs_symlink_inode_operations_dotl;
  261. else
  262. inode->i_op = &v9fs_symlink_inode_operations;
  263. break;
  264. case S_IFDIR:
  265. inc_nlink(inode);
  266. if (v9fs_proto_dotl(v9ses))
  267. inode->i_op = &v9fs_dir_inode_operations_dotl;
  268. else if (v9fs_proto_dotu(v9ses))
  269. inode->i_op = &v9fs_dir_inode_operations_dotu;
  270. else
  271. inode->i_op = &v9fs_dir_inode_operations;
  272. if (v9fs_proto_dotl(v9ses))
  273. inode->i_fop = &v9fs_dir_operations_dotl;
  274. else
  275. inode->i_fop = &v9fs_dir_operations;
  276. break;
  277. default:
  278. P9_DPRINTK(P9_DEBUG_ERROR, "BAD mode 0x%x S_IFMT 0x%x\n",
  279. mode, mode & S_IFMT);
  280. err = -EINVAL;
  281. goto error;
  282. }
  283. return inode;
  284. error:
  285. iput(inode);
  286. return ERR_PTR(err);
  287. }
  288. /*
  289. static struct v9fs_fid*
  290. v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
  291. {
  292. int err;
  293. int nfid;
  294. struct v9fs_fid *ret;
  295. struct v9fs_fcall *fcall;
  296. nfid = v9fs_get_idpool(&v9ses->fidpool);
  297. if (nfid < 0) {
  298. eprintk(KERN_WARNING, "no free fids available\n");
  299. return ERR_PTR(-ENOSPC);
  300. }
  301. err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
  302. &fcall);
  303. if (err < 0) {
  304. if (fcall && fcall->id == RWALK)
  305. goto clunk_fid;
  306. PRINT_FCALL_ERROR("walk error", fcall);
  307. v9fs_put_idpool(nfid, &v9ses->fidpool);
  308. goto error;
  309. }
  310. kfree(fcall);
  311. fcall = NULL;
  312. ret = v9fs_fid_create(v9ses, nfid);
  313. if (!ret) {
  314. err = -ENOMEM;
  315. goto clunk_fid;
  316. }
  317. err = v9fs_fid_insert(ret, dentry);
  318. if (err < 0) {
  319. v9fs_fid_destroy(ret);
  320. goto clunk_fid;
  321. }
  322. return ret;
  323. clunk_fid:
  324. v9fs_t_clunk(v9ses, nfid);
  325. error:
  326. kfree(fcall);
  327. return ERR_PTR(err);
  328. }
  329. */
  330. /**
  331. * v9fs_clear_inode - release an inode
  332. * @inode: inode to release
  333. *
  334. */
  335. void v9fs_clear_inode(struct inode *inode)
  336. {
  337. filemap_fdatawrite(inode->i_mapping);
  338. #ifdef CONFIG_9P_FSCACHE
  339. v9fs_cache_inode_put_cookie(inode);
  340. #endif
  341. }
  342. /**
  343. * v9fs_inode_from_fid - populate an inode by issuing a attribute request
  344. * @v9ses: session information
  345. * @fid: fid to issue attribute request for
  346. * @sb: superblock on which to create inode
  347. *
  348. */
  349. static struct inode *
  350. v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  351. struct super_block *sb)
  352. {
  353. int err, umode;
  354. struct inode *ret;
  355. struct p9_wstat *st;
  356. ret = NULL;
  357. st = p9_client_stat(fid);
  358. if (IS_ERR(st))
  359. return ERR_CAST(st);
  360. umode = p9mode2unixmode(v9ses, st->mode);
  361. ret = v9fs_get_inode(sb, umode);
  362. if (IS_ERR(ret)) {
  363. err = PTR_ERR(ret);
  364. goto error;
  365. }
  366. v9fs_stat2inode(st, ret, sb);
  367. ret->i_ino = v9fs_qid2ino(&st->qid);
  368. #ifdef CONFIG_9P_FSCACHE
  369. v9fs_vcookie_set_qid(ret, &st->qid);
  370. v9fs_cache_inode_get_cookie(ret);
  371. #endif
  372. p9stat_free(st);
  373. kfree(st);
  374. return ret;
  375. error:
  376. p9stat_free(st);
  377. kfree(st);
  378. return ERR_PTR(err);
  379. }
  380. /**
  381. * v9fs_remove - helper function to remove files and directories
  382. * @dir: directory inode that is being deleted
  383. * @file: dentry that is being deleted
  384. * @rmdir: removing a directory
  385. *
  386. */
  387. static int v9fs_remove(struct inode *dir, struct dentry *file, int rmdir)
  388. {
  389. int retval;
  390. struct inode *file_inode;
  391. struct v9fs_session_info *v9ses;
  392. struct p9_fid *v9fid;
  393. P9_DPRINTK(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %d\n", dir, file,
  394. rmdir);
  395. file_inode = file->d_inode;
  396. v9ses = v9fs_inode2v9ses(file_inode);
  397. v9fid = v9fs_fid_clone(file);
  398. if (IS_ERR(v9fid))
  399. return PTR_ERR(v9fid);
  400. retval = p9_client_remove(v9fid);
  401. if (!retval)
  402. drop_nlink(file_inode);
  403. return retval;
  404. }
  405. static int
  406. v9fs_open_created(struct inode *inode, struct file *file)
  407. {
  408. return 0;
  409. }
  410. /**
  411. * v9fs_create - Create a file
  412. * @v9ses: session information
  413. * @dir: directory that dentry is being created in
  414. * @dentry: dentry that is being created
  415. * @extension: 9p2000.u extension string to support devices, etc.
  416. * @perm: create permissions
  417. * @mode: open mode
  418. *
  419. */
  420. static struct p9_fid *
  421. v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
  422. struct dentry *dentry, char *extension, u32 perm, u8 mode)
  423. {
  424. int err;
  425. char *name;
  426. struct p9_fid *dfid, *ofid, *fid;
  427. struct inode *inode;
  428. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  429. err = 0;
  430. ofid = NULL;
  431. fid = NULL;
  432. name = (char *) dentry->d_name.name;
  433. dfid = v9fs_fid_clone(dentry->d_parent);
  434. if (IS_ERR(dfid)) {
  435. err = PTR_ERR(dfid);
  436. P9_DPRINTK(P9_DEBUG_VFS, "fid clone failed %d\n", err);
  437. dfid = NULL;
  438. goto error;
  439. }
  440. /* clone a fid to use for creation */
  441. ofid = p9_client_walk(dfid, 0, NULL, 1);
  442. if (IS_ERR(ofid)) {
  443. err = PTR_ERR(ofid);
  444. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  445. ofid = NULL;
  446. goto error;
  447. }
  448. err = p9_client_fcreate(ofid, name, perm, mode, extension);
  449. if (err < 0) {
  450. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
  451. goto error;
  452. }
  453. /* now walk from the parent so we can get unopened fid */
  454. fid = p9_client_walk(dfid, 1, &name, 0);
  455. if (IS_ERR(fid)) {
  456. err = PTR_ERR(fid);
  457. P9_DPRINTK(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  458. fid = NULL;
  459. goto error;
  460. } else
  461. dfid = NULL;
  462. /* instantiate inode and assign the unopened fid to the dentry */
  463. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  464. if (IS_ERR(inode)) {
  465. err = PTR_ERR(inode);
  466. P9_DPRINTK(P9_DEBUG_VFS, "inode creation failed %d\n", err);
  467. goto error;
  468. }
  469. if (v9ses->cache)
  470. dentry->d_op = &v9fs_cached_dentry_operations;
  471. else
  472. dentry->d_op = &v9fs_dentry_operations;
  473. d_instantiate(dentry, inode);
  474. err = v9fs_fid_add(dentry, fid);
  475. if (err < 0)
  476. goto error;
  477. return ofid;
  478. error:
  479. if (dfid)
  480. p9_client_clunk(dfid);
  481. if (ofid)
  482. p9_client_clunk(ofid);
  483. if (fid)
  484. p9_client_clunk(fid);
  485. return ERR_PTR(err);
  486. }
  487. /**
  488. * v9fs_vfs_create - VFS hook to create files
  489. * @dir: directory inode that is being created
  490. * @dentry: dentry that is being deleted
  491. * @mode: create permissions
  492. * @nd: path information
  493. *
  494. */
  495. static int
  496. v9fs_vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  497. struct nameidata *nd)
  498. {
  499. int err;
  500. u32 perm;
  501. int flags;
  502. struct v9fs_session_info *v9ses;
  503. struct p9_fid *fid;
  504. struct file *filp;
  505. err = 0;
  506. fid = NULL;
  507. v9ses = v9fs_inode2v9ses(dir);
  508. perm = unixmode2p9mode(v9ses, mode);
  509. if (nd && nd->flags & LOOKUP_OPEN)
  510. flags = nd->intent.open.flags - 1;
  511. else
  512. flags = O_RDWR;
  513. fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
  514. v9fs_uflags2omode(flags,
  515. v9fs_proto_dotu(v9ses)));
  516. if (IS_ERR(fid)) {
  517. err = PTR_ERR(fid);
  518. fid = NULL;
  519. goto error;
  520. }
  521. /* if we are opening a file, assign the open fid to the file */
  522. if (nd && nd->flags & LOOKUP_OPEN) {
  523. filp = lookup_instantiate_filp(nd, dentry, v9fs_open_created);
  524. if (IS_ERR(filp)) {
  525. err = PTR_ERR(filp);
  526. goto error;
  527. }
  528. filp->private_data = fid;
  529. } else
  530. p9_client_clunk(fid);
  531. return 0;
  532. error:
  533. if (fid)
  534. p9_client_clunk(fid);
  535. return err;
  536. }
  537. /**
  538. * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
  539. * @dir: inode that is being unlinked
  540. * @dentry: dentry that is being unlinked
  541. * @mode: mode for new directory
  542. *
  543. */
  544. static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  545. {
  546. int err;
  547. u32 perm;
  548. struct v9fs_session_info *v9ses;
  549. struct p9_fid *fid;
  550. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  551. err = 0;
  552. v9ses = v9fs_inode2v9ses(dir);
  553. perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
  554. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
  555. if (IS_ERR(fid)) {
  556. err = PTR_ERR(fid);
  557. fid = NULL;
  558. }
  559. if (fid)
  560. p9_client_clunk(fid);
  561. return err;
  562. }
  563. /**
  564. * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
  565. * @dir: inode that is being walked from
  566. * @dentry: dentry that is being walked to?
  567. * @nameidata: path data
  568. *
  569. */
  570. static struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
  571. struct nameidata *nameidata)
  572. {
  573. struct super_block *sb;
  574. struct v9fs_session_info *v9ses;
  575. struct p9_fid *dfid, *fid;
  576. struct inode *inode;
  577. char *name;
  578. int result = 0;
  579. P9_DPRINTK(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
  580. dir, dentry->d_name.name, dentry, nameidata);
  581. if (dentry->d_name.len > NAME_MAX)
  582. return ERR_PTR(-ENAMETOOLONG);
  583. sb = dir->i_sb;
  584. v9ses = v9fs_inode2v9ses(dir);
  585. dfid = v9fs_fid_lookup(dentry->d_parent);
  586. if (IS_ERR(dfid))
  587. return ERR_CAST(dfid);
  588. name = (char *) dentry->d_name.name;
  589. fid = p9_client_walk(dfid, 1, &name, 1);
  590. if (IS_ERR(fid)) {
  591. result = PTR_ERR(fid);
  592. if (result == -ENOENT) {
  593. d_add(dentry, NULL);
  594. return NULL;
  595. }
  596. return ERR_PTR(result);
  597. }
  598. inode = v9fs_inode_from_fid(v9ses, fid, dir->i_sb);
  599. if (IS_ERR(inode)) {
  600. result = PTR_ERR(inode);
  601. inode = NULL;
  602. goto error;
  603. }
  604. result = v9fs_fid_add(dentry, fid);
  605. if (result < 0)
  606. goto error;
  607. if ((fid->qid.version) && (v9ses->cache))
  608. dentry->d_op = &v9fs_cached_dentry_operations;
  609. else
  610. dentry->d_op = &v9fs_dentry_operations;
  611. d_add(dentry, inode);
  612. return NULL;
  613. error:
  614. p9_client_clunk(fid);
  615. return ERR_PTR(result);
  616. }
  617. /**
  618. * v9fs_vfs_unlink - VFS unlink hook to delete an inode
  619. * @i: inode that is being unlinked
  620. * @d: dentry that is being unlinked
  621. *
  622. */
  623. static int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
  624. {
  625. return v9fs_remove(i, d, 0);
  626. }
  627. /**
  628. * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
  629. * @i: inode that is being unlinked
  630. * @d: dentry that is being unlinked
  631. *
  632. */
  633. static int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
  634. {
  635. return v9fs_remove(i, d, 1);
  636. }
  637. /**
  638. * v9fs_vfs_rename - VFS hook to rename an inode
  639. * @old_dir: old dir inode
  640. * @old_dentry: old dentry
  641. * @new_dir: new dir inode
  642. * @new_dentry: new dentry
  643. *
  644. */
  645. static int
  646. v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  647. struct inode *new_dir, struct dentry *new_dentry)
  648. {
  649. struct inode *old_inode;
  650. struct v9fs_session_info *v9ses;
  651. struct p9_fid *oldfid;
  652. struct p9_fid *olddirfid;
  653. struct p9_fid *newdirfid;
  654. struct p9_wstat wstat;
  655. int retval;
  656. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  657. retval = 0;
  658. old_inode = old_dentry->d_inode;
  659. v9ses = v9fs_inode2v9ses(old_inode);
  660. oldfid = v9fs_fid_lookup(old_dentry);
  661. if (IS_ERR(oldfid))
  662. return PTR_ERR(oldfid);
  663. olddirfid = v9fs_fid_clone(old_dentry->d_parent);
  664. if (IS_ERR(olddirfid)) {
  665. retval = PTR_ERR(olddirfid);
  666. goto done;
  667. }
  668. newdirfid = v9fs_fid_clone(new_dentry->d_parent);
  669. if (IS_ERR(newdirfid)) {
  670. retval = PTR_ERR(newdirfid);
  671. goto clunk_olddir;
  672. }
  673. if (v9fs_proto_dotl(v9ses)) {
  674. retval = p9_client_rename(oldfid, newdirfid,
  675. (char *) new_dentry->d_name.name);
  676. if (retval != -ENOSYS)
  677. goto clunk_newdir;
  678. }
  679. /* 9P can only handle file rename in the same directory */
  680. if (memcmp(&olddirfid->qid, &newdirfid->qid, sizeof(newdirfid->qid))) {
  681. P9_DPRINTK(P9_DEBUG_ERROR,
  682. "old dir and new dir are different\n");
  683. retval = -EXDEV;
  684. goto clunk_newdir;
  685. }
  686. v9fs_blank_wstat(&wstat);
  687. wstat.muid = v9ses->uname;
  688. wstat.name = (char *) new_dentry->d_name.name;
  689. retval = p9_client_wstat(oldfid, &wstat);
  690. clunk_newdir:
  691. p9_client_clunk(newdirfid);
  692. clunk_olddir:
  693. p9_client_clunk(olddirfid);
  694. done:
  695. return retval;
  696. }
  697. /**
  698. * v9fs_vfs_getattr - retrieve file metadata
  699. * @mnt: mount information
  700. * @dentry: file to get attributes on
  701. * @stat: metadata structure to populate
  702. *
  703. */
  704. static int
  705. v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  706. struct kstat *stat)
  707. {
  708. int err;
  709. struct v9fs_session_info *v9ses;
  710. struct p9_fid *fid;
  711. struct p9_wstat *st;
  712. P9_DPRINTK(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  713. err = -EPERM;
  714. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  715. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
  716. return simple_getattr(mnt, dentry, stat);
  717. fid = v9fs_fid_lookup(dentry);
  718. if (IS_ERR(fid))
  719. return PTR_ERR(fid);
  720. st = p9_client_stat(fid);
  721. if (IS_ERR(st))
  722. return PTR_ERR(st);
  723. v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
  724. generic_fillattr(dentry->d_inode, stat);
  725. kfree(st);
  726. return 0;
  727. }
  728. /**
  729. * v9fs_vfs_setattr - set file metadata
  730. * @dentry: file whose metadata to set
  731. * @iattr: metadata assignment structure
  732. *
  733. */
  734. static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
  735. {
  736. int retval;
  737. struct v9fs_session_info *v9ses;
  738. struct p9_fid *fid;
  739. struct p9_wstat wstat;
  740. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  741. retval = -EPERM;
  742. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  743. fid = v9fs_fid_lookup(dentry);
  744. if(IS_ERR(fid))
  745. return PTR_ERR(fid);
  746. v9fs_blank_wstat(&wstat);
  747. if (iattr->ia_valid & ATTR_MODE)
  748. wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
  749. if (iattr->ia_valid & ATTR_MTIME)
  750. wstat.mtime = iattr->ia_mtime.tv_sec;
  751. if (iattr->ia_valid & ATTR_ATIME)
  752. wstat.atime = iattr->ia_atime.tv_sec;
  753. if (iattr->ia_valid & ATTR_SIZE)
  754. wstat.length = iattr->ia_size;
  755. if (v9fs_proto_dotu(v9ses)) {
  756. if (iattr->ia_valid & ATTR_UID)
  757. wstat.n_uid = iattr->ia_uid;
  758. if (iattr->ia_valid & ATTR_GID)
  759. wstat.n_gid = iattr->ia_gid;
  760. }
  761. retval = p9_client_wstat(fid, &wstat);
  762. if (retval >= 0)
  763. retval = inode_setattr(dentry->d_inode, iattr);
  764. return retval;
  765. }
  766. /**
  767. * v9fs_stat2inode - populate an inode structure with mistat info
  768. * @stat: Plan 9 metadata (mistat) structure
  769. * @inode: inode to populate
  770. * @sb: superblock of filesystem
  771. *
  772. */
  773. void
  774. v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
  775. struct super_block *sb)
  776. {
  777. char ext[32];
  778. char tag_name[14];
  779. unsigned int i_nlink;
  780. struct v9fs_session_info *v9ses = sb->s_fs_info;
  781. inode->i_nlink = 1;
  782. inode->i_atime.tv_sec = stat->atime;
  783. inode->i_mtime.tv_sec = stat->mtime;
  784. inode->i_ctime.tv_sec = stat->mtime;
  785. inode->i_uid = v9ses->dfltuid;
  786. inode->i_gid = v9ses->dfltgid;
  787. if (v9fs_proto_dotu(v9ses)) {
  788. inode->i_uid = stat->n_uid;
  789. inode->i_gid = stat->n_gid;
  790. }
  791. if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
  792. if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
  793. /*
  794. * Hadlink support got added later to
  795. * to the .u extension. So there can be
  796. * server out there that doesn't support
  797. * this even with .u extension. So check
  798. * for non NULL stat->extension
  799. */
  800. strncpy(ext, stat->extension, sizeof(ext));
  801. /* HARDLINKCOUNT %u */
  802. sscanf(ext, "%13s %u", tag_name, &i_nlink);
  803. if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
  804. inode->i_nlink = i_nlink;
  805. }
  806. }
  807. inode->i_mode = p9mode2unixmode(v9ses, stat->mode);
  808. if ((S_ISBLK(inode->i_mode)) || (S_ISCHR(inode->i_mode))) {
  809. char type = 0;
  810. int major = -1;
  811. int minor = -1;
  812. strncpy(ext, stat->extension, sizeof(ext));
  813. sscanf(ext, "%c %u %u", &type, &major, &minor);
  814. switch (type) {
  815. case 'c':
  816. inode->i_mode &= ~S_IFBLK;
  817. inode->i_mode |= S_IFCHR;
  818. break;
  819. case 'b':
  820. break;
  821. default:
  822. P9_DPRINTK(P9_DEBUG_ERROR,
  823. "Unknown special type %c %s\n", type,
  824. stat->extension);
  825. };
  826. inode->i_rdev = MKDEV(major, minor);
  827. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  828. } else
  829. inode->i_rdev = 0;
  830. i_size_write(inode, stat->length);
  831. /* not real number of blocks, but 512 byte ones ... */
  832. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  833. }
  834. /**
  835. * v9fs_qid2ino - convert qid into inode number
  836. * @qid: qid to hash
  837. *
  838. * BUG: potential for inode number collisions?
  839. */
  840. ino_t v9fs_qid2ino(struct p9_qid *qid)
  841. {
  842. u64 path = qid->path + 2;
  843. ino_t i = 0;
  844. if (sizeof(ino_t) == sizeof(path))
  845. memcpy(&i, &path, sizeof(ino_t));
  846. else
  847. i = (ino_t) (path ^ (path >> 32));
  848. return i;
  849. }
  850. /**
  851. * v9fs_readlink - read a symlink's location (internal version)
  852. * @dentry: dentry for symlink
  853. * @buffer: buffer to load symlink location into
  854. * @buflen: length of buffer
  855. *
  856. */
  857. static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
  858. {
  859. int retval;
  860. struct v9fs_session_info *v9ses;
  861. struct p9_fid *fid;
  862. struct p9_wstat *st;
  863. P9_DPRINTK(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
  864. retval = -EPERM;
  865. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  866. fid = v9fs_fid_lookup(dentry);
  867. if (IS_ERR(fid))
  868. return PTR_ERR(fid);
  869. if (!v9fs_proto_dotu(v9ses))
  870. return -EBADF;
  871. st = p9_client_stat(fid);
  872. if (IS_ERR(st))
  873. return PTR_ERR(st);
  874. if (!(st->mode & P9_DMSYMLINK)) {
  875. retval = -EINVAL;
  876. goto done;
  877. }
  878. /* copy extension buffer into buffer */
  879. strncpy(buffer, st->extension, buflen);
  880. P9_DPRINTK(P9_DEBUG_VFS,
  881. "%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
  882. retval = strnlen(buffer, buflen);
  883. done:
  884. kfree(st);
  885. return retval;
  886. }
  887. /**
  888. * v9fs_vfs_follow_link - follow a symlink path
  889. * @dentry: dentry for symlink
  890. * @nd: nameidata
  891. *
  892. */
  893. static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  894. {
  895. int len = 0;
  896. char *link = __getname();
  897. P9_DPRINTK(P9_DEBUG_VFS, "%s n", dentry->d_name.name);
  898. if (!link)
  899. link = ERR_PTR(-ENOMEM);
  900. else {
  901. len = v9fs_readlink(dentry, link, PATH_MAX);
  902. if (len < 0) {
  903. __putname(link);
  904. link = ERR_PTR(len);
  905. } else
  906. link[min(len, PATH_MAX-1)] = 0;
  907. }
  908. nd_set_link(nd, link);
  909. return NULL;
  910. }
  911. /**
  912. * v9fs_vfs_put_link - release a symlink path
  913. * @dentry: dentry for symlink
  914. * @nd: nameidata
  915. * @p: unused
  916. *
  917. */
  918. static void
  919. v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  920. {
  921. char *s = nd_get_link(nd);
  922. P9_DPRINTK(P9_DEBUG_VFS, " %s %s\n", dentry->d_name.name,
  923. IS_ERR(s) ? "<error>" : s);
  924. if (!IS_ERR(s))
  925. __putname(s);
  926. }
  927. /**
  928. * v9fs_vfs_mkspecial - create a special file
  929. * @dir: inode to create special file in
  930. * @dentry: dentry to create
  931. * @mode: mode to create special file
  932. * @extension: 9p2000.u format extension string representing special file
  933. *
  934. */
  935. static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
  936. int mode, const char *extension)
  937. {
  938. u32 perm;
  939. struct v9fs_session_info *v9ses;
  940. struct p9_fid *fid;
  941. v9ses = v9fs_inode2v9ses(dir);
  942. if (!v9fs_proto_dotu(v9ses)) {
  943. P9_DPRINTK(P9_DEBUG_ERROR, "not extended\n");
  944. return -EPERM;
  945. }
  946. perm = unixmode2p9mode(v9ses, mode);
  947. fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
  948. P9_OREAD);
  949. if (IS_ERR(fid))
  950. return PTR_ERR(fid);
  951. p9_client_clunk(fid);
  952. return 0;
  953. }
  954. /**
  955. * v9fs_vfs_symlink - helper function to create symlinks
  956. * @dir: directory inode containing symlink
  957. * @dentry: dentry for symlink
  958. * @symname: symlink data
  959. *
  960. * See Also: 9P2000.u RFC for more information
  961. *
  962. */
  963. static int
  964. v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  965. {
  966. P9_DPRINTK(P9_DEBUG_VFS, " %lu,%s,%s\n", dir->i_ino,
  967. dentry->d_name.name, symname);
  968. return v9fs_vfs_mkspecial(dir, dentry, S_IFLNK, symname);
  969. }
  970. /**
  971. * v9fs_vfs_link - create a hardlink
  972. * @old_dentry: dentry for file to link to
  973. * @dir: inode destination for new link
  974. * @dentry: dentry for link
  975. *
  976. */
  977. static int
  978. v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
  979. struct dentry *dentry)
  980. {
  981. int retval;
  982. struct p9_fid *oldfid;
  983. char *name;
  984. P9_DPRINTK(P9_DEBUG_VFS,
  985. " %lu,%s,%s\n", dir->i_ino, dentry->d_name.name,
  986. old_dentry->d_name.name);
  987. oldfid = v9fs_fid_clone(old_dentry);
  988. if (IS_ERR(oldfid))
  989. return PTR_ERR(oldfid);
  990. name = __getname();
  991. if (unlikely(!name)) {
  992. retval = -ENOMEM;
  993. goto clunk_fid;
  994. }
  995. sprintf(name, "%d\n", oldfid->fid);
  996. retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
  997. __putname(name);
  998. clunk_fid:
  999. p9_client_clunk(oldfid);
  1000. return retval;
  1001. }
  1002. /**
  1003. * v9fs_vfs_mknod - create a special file
  1004. * @dir: inode destination for new link
  1005. * @dentry: dentry for file
  1006. * @mode: mode for creation
  1007. * @rdev: device associated with special file
  1008. *
  1009. */
  1010. static int
  1011. v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1012. {
  1013. int retval;
  1014. char *name;
  1015. P9_DPRINTK(P9_DEBUG_VFS,
  1016. " %lu,%s mode: %x MAJOR: %u MINOR: %u\n", dir->i_ino,
  1017. dentry->d_name.name, mode, MAJOR(rdev), MINOR(rdev));
  1018. if (!new_valid_dev(rdev))
  1019. return -EINVAL;
  1020. name = __getname();
  1021. if (!name)
  1022. return -ENOMEM;
  1023. /* build extension */
  1024. if (S_ISBLK(mode))
  1025. sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
  1026. else if (S_ISCHR(mode))
  1027. sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
  1028. else if (S_ISFIFO(mode))
  1029. *name = 0;
  1030. else {
  1031. __putname(name);
  1032. return -EINVAL;
  1033. }
  1034. retval = v9fs_vfs_mkspecial(dir, dentry, mode, name);
  1035. __putname(name);
  1036. return retval;
  1037. }
  1038. static const struct inode_operations v9fs_dir_inode_operations_dotu = {
  1039. .create = v9fs_vfs_create,
  1040. .lookup = v9fs_vfs_lookup,
  1041. .symlink = v9fs_vfs_symlink,
  1042. .link = v9fs_vfs_link,
  1043. .unlink = v9fs_vfs_unlink,
  1044. .mkdir = v9fs_vfs_mkdir,
  1045. .rmdir = v9fs_vfs_rmdir,
  1046. .mknod = v9fs_vfs_mknod,
  1047. .rename = v9fs_vfs_rename,
  1048. .getattr = v9fs_vfs_getattr,
  1049. .setattr = v9fs_vfs_setattr,
  1050. };
  1051. static const struct inode_operations v9fs_dir_inode_operations_dotl = {
  1052. .create = v9fs_vfs_create,
  1053. .lookup = v9fs_vfs_lookup,
  1054. .symlink = v9fs_vfs_symlink,
  1055. .link = v9fs_vfs_link,
  1056. .unlink = v9fs_vfs_unlink,
  1057. .mkdir = v9fs_vfs_mkdir,
  1058. .rmdir = v9fs_vfs_rmdir,
  1059. .mknod = v9fs_vfs_mknod,
  1060. .rename = v9fs_vfs_rename,
  1061. .getattr = v9fs_vfs_getattr,
  1062. .setattr = v9fs_vfs_setattr,
  1063. };
  1064. static const struct inode_operations v9fs_dir_inode_operations = {
  1065. .create = v9fs_vfs_create,
  1066. .lookup = v9fs_vfs_lookup,
  1067. .unlink = v9fs_vfs_unlink,
  1068. .mkdir = v9fs_vfs_mkdir,
  1069. .rmdir = v9fs_vfs_rmdir,
  1070. .mknod = v9fs_vfs_mknod,
  1071. .rename = v9fs_vfs_rename,
  1072. .getattr = v9fs_vfs_getattr,
  1073. .setattr = v9fs_vfs_setattr,
  1074. };
  1075. static const struct inode_operations v9fs_file_inode_operations = {
  1076. .getattr = v9fs_vfs_getattr,
  1077. .setattr = v9fs_vfs_setattr,
  1078. };
  1079. static const struct inode_operations v9fs_file_inode_operations_dotl = {
  1080. .getattr = v9fs_vfs_getattr,
  1081. .setattr = v9fs_vfs_setattr,
  1082. };
  1083. static const struct inode_operations v9fs_symlink_inode_operations = {
  1084. .readlink = generic_readlink,
  1085. .follow_link = v9fs_vfs_follow_link,
  1086. .put_link = v9fs_vfs_put_link,
  1087. .getattr = v9fs_vfs_getattr,
  1088. .setattr = v9fs_vfs_setattr,
  1089. };
  1090. static const struct inode_operations v9fs_symlink_inode_operations_dotl = {
  1091. .readlink = generic_readlink,
  1092. .follow_link = v9fs_vfs_follow_link,
  1093. .put_link = v9fs_vfs_put_link,
  1094. .getattr = v9fs_vfs_getattr,
  1095. .setattr = v9fs_vfs_setattr,
  1096. };