vfs_inode.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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/inet.h>
  34. #include <linux/namei.h>
  35. #include <linux/idr.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/xattr.h>
  39. #include <linux/posix_acl.h>
  40. #include <net/9p/9p.h>
  41. #include <net/9p/client.h>
  42. #include "v9fs.h"
  43. #include "v9fs_vfs.h"
  44. #include "fid.h"
  45. #include "cache.h"
  46. #include "xattr.h"
  47. #include "acl.h"
  48. static const struct inode_operations v9fs_dir_inode_operations;
  49. static const struct inode_operations v9fs_dir_inode_operations_dotu;
  50. static const struct inode_operations v9fs_file_inode_operations;
  51. static const struct inode_operations v9fs_symlink_inode_operations;
  52. /**
  53. * unixmode2p9mode - convert unix mode bits to plan 9
  54. * @v9ses: v9fs session information
  55. * @mode: mode to convert
  56. *
  57. */
  58. static u32 unixmode2p9mode(struct v9fs_session_info *v9ses, umode_t mode)
  59. {
  60. int res;
  61. res = mode & 0777;
  62. if (S_ISDIR(mode))
  63. res |= P9_DMDIR;
  64. if (v9fs_proto_dotu(v9ses)) {
  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. }
  82. return res;
  83. }
  84. /**
  85. * p9mode2perm- convert plan9 mode bits to unix permission bits
  86. * @v9ses: v9fs session information
  87. * @stat: p9_wstat from which mode need to be derived
  88. *
  89. */
  90. static int p9mode2perm(struct v9fs_session_info *v9ses,
  91. struct p9_wstat *stat)
  92. {
  93. int res;
  94. int mode = stat->mode;
  95. res = mode & S_IALLUGO;
  96. if (v9fs_proto_dotu(v9ses)) {
  97. if ((mode & P9_DMSETUID) == P9_DMSETUID)
  98. res |= S_ISUID;
  99. if ((mode & P9_DMSETGID) == P9_DMSETGID)
  100. res |= S_ISGID;
  101. if ((mode & P9_DMSETVTX) == P9_DMSETVTX)
  102. res |= S_ISVTX;
  103. }
  104. return res;
  105. }
  106. /**
  107. * p9mode2unixmode- convert plan9 mode bits to unix mode bits
  108. * @v9ses: v9fs session information
  109. * @stat: p9_wstat from which mode need to be derived
  110. * @rdev: major number, minor number in case of device files.
  111. *
  112. */
  113. static umode_t p9mode2unixmode(struct v9fs_session_info *v9ses,
  114. struct p9_wstat *stat, dev_t *rdev)
  115. {
  116. int res;
  117. u32 mode = stat->mode;
  118. *rdev = 0;
  119. res = p9mode2perm(v9ses, stat);
  120. if ((mode & P9_DMDIR) == P9_DMDIR)
  121. res |= S_IFDIR;
  122. else if ((mode & P9_DMSYMLINK) && (v9fs_proto_dotu(v9ses)))
  123. res |= S_IFLNK;
  124. else if ((mode & P9_DMSOCKET) && (v9fs_proto_dotu(v9ses))
  125. && (v9ses->nodev == 0))
  126. res |= S_IFSOCK;
  127. else if ((mode & P9_DMNAMEDPIPE) && (v9fs_proto_dotu(v9ses))
  128. && (v9ses->nodev == 0))
  129. res |= S_IFIFO;
  130. else if ((mode & P9_DMDEVICE) && (v9fs_proto_dotu(v9ses))
  131. && (v9ses->nodev == 0)) {
  132. char type = 0, ext[32];
  133. int major = -1, minor = -1;
  134. strncpy(ext, stat->extension, sizeof(ext));
  135. sscanf(ext, "%c %u %u", &type, &major, &minor);
  136. switch (type) {
  137. case 'c':
  138. res |= S_IFCHR;
  139. break;
  140. case 'b':
  141. res |= S_IFBLK;
  142. break;
  143. default:
  144. p9_debug(P9_DEBUG_ERROR, "Unknown special type %c %s\n",
  145. type, stat->extension);
  146. };
  147. *rdev = MKDEV(major, minor);
  148. } else
  149. res |= S_IFREG;
  150. return res;
  151. }
  152. /**
  153. * v9fs_uflags2omode- convert posix open flags to plan 9 mode bits
  154. * @uflags: flags to convert
  155. * @extended: if .u extensions are active
  156. */
  157. int v9fs_uflags2omode(int uflags, int extended)
  158. {
  159. int ret;
  160. ret = 0;
  161. switch (uflags&3) {
  162. default:
  163. case O_RDONLY:
  164. ret = P9_OREAD;
  165. break;
  166. case O_WRONLY:
  167. ret = P9_OWRITE;
  168. break;
  169. case O_RDWR:
  170. ret = P9_ORDWR;
  171. break;
  172. }
  173. if (uflags & O_TRUNC)
  174. ret |= P9_OTRUNC;
  175. if (extended) {
  176. if (uflags & O_EXCL)
  177. ret |= P9_OEXCL;
  178. if (uflags & O_APPEND)
  179. ret |= P9_OAPPEND;
  180. }
  181. return ret;
  182. }
  183. /**
  184. * v9fs_blank_wstat - helper function to setup a 9P stat structure
  185. * @wstat: structure to initialize
  186. *
  187. */
  188. void
  189. v9fs_blank_wstat(struct p9_wstat *wstat)
  190. {
  191. wstat->type = ~0;
  192. wstat->dev = ~0;
  193. wstat->qid.type = ~0;
  194. wstat->qid.version = ~0;
  195. *((long long *)&wstat->qid.path) = ~0;
  196. wstat->mode = ~0;
  197. wstat->atime = ~0;
  198. wstat->mtime = ~0;
  199. wstat->length = ~0;
  200. wstat->name = NULL;
  201. wstat->uid = NULL;
  202. wstat->gid = NULL;
  203. wstat->muid = NULL;
  204. wstat->n_uid = ~0;
  205. wstat->n_gid = ~0;
  206. wstat->n_muid = ~0;
  207. wstat->extension = NULL;
  208. }
  209. /**
  210. * v9fs_alloc_inode - helper function to allocate an inode
  211. *
  212. */
  213. struct inode *v9fs_alloc_inode(struct super_block *sb)
  214. {
  215. struct v9fs_inode *v9inode;
  216. v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
  217. GFP_KERNEL);
  218. if (!v9inode)
  219. return NULL;
  220. #ifdef CONFIG_9P_FSCACHE
  221. v9inode->fscache = NULL;
  222. spin_lock_init(&v9inode->fscache_lock);
  223. #endif
  224. v9inode->writeback_fid = NULL;
  225. v9inode->cache_validity = 0;
  226. mutex_init(&v9inode->v_mutex);
  227. return &v9inode->vfs_inode;
  228. }
  229. /**
  230. * v9fs_destroy_inode - destroy an inode
  231. *
  232. */
  233. static void v9fs_i_callback(struct rcu_head *head)
  234. {
  235. struct inode *inode = container_of(head, struct inode, i_rcu);
  236. kmem_cache_free(v9fs_inode_cache, V9FS_I(inode));
  237. }
  238. void v9fs_destroy_inode(struct inode *inode)
  239. {
  240. call_rcu(&inode->i_rcu, v9fs_i_callback);
  241. }
  242. int v9fs_init_inode(struct v9fs_session_info *v9ses,
  243. struct inode *inode, umode_t mode, dev_t rdev)
  244. {
  245. int err = 0;
  246. inode_init_owner(inode, NULL, mode);
  247. inode->i_blocks = 0;
  248. inode->i_rdev = rdev;
  249. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  250. inode->i_mapping->a_ops = &v9fs_addr_operations;
  251. switch (mode & S_IFMT) {
  252. case S_IFIFO:
  253. case S_IFBLK:
  254. case S_IFCHR:
  255. case S_IFSOCK:
  256. if (v9fs_proto_dotl(v9ses)) {
  257. inode->i_op = &v9fs_file_inode_operations_dotl;
  258. } else if (v9fs_proto_dotu(v9ses)) {
  259. inode->i_op = &v9fs_file_inode_operations;
  260. } else {
  261. p9_debug(P9_DEBUG_ERROR,
  262. "special files without extended mode\n");
  263. err = -EINVAL;
  264. goto error;
  265. }
  266. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  267. break;
  268. case S_IFREG:
  269. if (v9fs_proto_dotl(v9ses)) {
  270. inode->i_op = &v9fs_file_inode_operations_dotl;
  271. if (v9ses->cache)
  272. inode->i_fop =
  273. &v9fs_cached_file_operations_dotl;
  274. else
  275. inode->i_fop = &v9fs_file_operations_dotl;
  276. } else {
  277. inode->i_op = &v9fs_file_inode_operations;
  278. if (v9ses->cache)
  279. inode->i_fop = &v9fs_cached_file_operations;
  280. else
  281. inode->i_fop = &v9fs_file_operations;
  282. }
  283. break;
  284. case S_IFLNK:
  285. if (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)) {
  286. p9_debug(P9_DEBUG_ERROR,
  287. "extended modes used with legacy protocol\n");
  288. err = -EINVAL;
  289. goto error;
  290. }
  291. if (v9fs_proto_dotl(v9ses))
  292. inode->i_op = &v9fs_symlink_inode_operations_dotl;
  293. else
  294. inode->i_op = &v9fs_symlink_inode_operations;
  295. break;
  296. case S_IFDIR:
  297. inc_nlink(inode);
  298. if (v9fs_proto_dotl(v9ses))
  299. inode->i_op = &v9fs_dir_inode_operations_dotl;
  300. else if (v9fs_proto_dotu(v9ses))
  301. inode->i_op = &v9fs_dir_inode_operations_dotu;
  302. else
  303. inode->i_op = &v9fs_dir_inode_operations;
  304. if (v9fs_proto_dotl(v9ses))
  305. inode->i_fop = &v9fs_dir_operations_dotl;
  306. else
  307. inode->i_fop = &v9fs_dir_operations;
  308. break;
  309. default:
  310. p9_debug(P9_DEBUG_ERROR, "BAD mode 0x%hx S_IFMT 0x%x\n",
  311. mode, mode & S_IFMT);
  312. err = -EINVAL;
  313. goto error;
  314. }
  315. error:
  316. return err;
  317. }
  318. /**
  319. * v9fs_get_inode - helper function to setup an inode
  320. * @sb: superblock
  321. * @mode: mode to setup inode with
  322. *
  323. */
  324. struct inode *v9fs_get_inode(struct super_block *sb, umode_t mode, dev_t rdev)
  325. {
  326. int err;
  327. struct inode *inode;
  328. struct v9fs_session_info *v9ses = sb->s_fs_info;
  329. p9_debug(P9_DEBUG_VFS, "super block: %p mode: %ho\n", sb, mode);
  330. inode = new_inode(sb);
  331. if (!inode) {
  332. pr_warn("%s (%d): Problem allocating inode\n",
  333. __func__, task_pid_nr(current));
  334. return ERR_PTR(-ENOMEM);
  335. }
  336. err = v9fs_init_inode(v9ses, inode, mode, rdev);
  337. if (err) {
  338. iput(inode);
  339. return ERR_PTR(err);
  340. }
  341. return inode;
  342. }
  343. /*
  344. static struct v9fs_fid*
  345. v9fs_clone_walk(struct v9fs_session_info *v9ses, u32 fid, struct dentry *dentry)
  346. {
  347. int err;
  348. int nfid;
  349. struct v9fs_fid *ret;
  350. struct v9fs_fcall *fcall;
  351. nfid = v9fs_get_idpool(&v9ses->fidpool);
  352. if (nfid < 0) {
  353. eprintk(KERN_WARNING, "no free fids available\n");
  354. return ERR_PTR(-ENOSPC);
  355. }
  356. err = v9fs_t_walk(v9ses, fid, nfid, (char *) dentry->d_name.name,
  357. &fcall);
  358. if (err < 0) {
  359. if (fcall && fcall->id == RWALK)
  360. goto clunk_fid;
  361. PRINT_FCALL_ERROR("walk error", fcall);
  362. v9fs_put_idpool(nfid, &v9ses->fidpool);
  363. goto error;
  364. }
  365. kfree(fcall);
  366. fcall = NULL;
  367. ret = v9fs_fid_create(v9ses, nfid);
  368. if (!ret) {
  369. err = -ENOMEM;
  370. goto clunk_fid;
  371. }
  372. err = v9fs_fid_insert(ret, dentry);
  373. if (err < 0) {
  374. v9fs_fid_destroy(ret);
  375. goto clunk_fid;
  376. }
  377. return ret;
  378. clunk_fid:
  379. v9fs_t_clunk(v9ses, nfid);
  380. error:
  381. kfree(fcall);
  382. return ERR_PTR(err);
  383. }
  384. */
  385. /**
  386. * v9fs_clear_inode - release an inode
  387. * @inode: inode to release
  388. *
  389. */
  390. void v9fs_evict_inode(struct inode *inode)
  391. {
  392. struct v9fs_inode *v9inode = V9FS_I(inode);
  393. truncate_inode_pages(&inode->i_data, 0);
  394. end_writeback(inode);
  395. filemap_fdatawrite(&inode->i_data);
  396. #ifdef CONFIG_9P_FSCACHE
  397. v9fs_cache_inode_put_cookie(inode);
  398. #endif
  399. /* clunk the fid stashed in writeback_fid */
  400. if (v9inode->writeback_fid) {
  401. p9_client_clunk(v9inode->writeback_fid);
  402. v9inode->writeback_fid = NULL;
  403. }
  404. }
  405. static int v9fs_test_inode(struct inode *inode, void *data)
  406. {
  407. int umode;
  408. dev_t rdev;
  409. struct v9fs_inode *v9inode = V9FS_I(inode);
  410. struct p9_wstat *st = (struct p9_wstat *)data;
  411. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  412. umode = p9mode2unixmode(v9ses, st, &rdev);
  413. /* don't match inode of different type */
  414. if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
  415. return 0;
  416. /* compare qid details */
  417. if (memcmp(&v9inode->qid.version,
  418. &st->qid.version, sizeof(v9inode->qid.version)))
  419. return 0;
  420. if (v9inode->qid.type != st->qid.type)
  421. return 0;
  422. return 1;
  423. }
  424. static int v9fs_test_new_inode(struct inode *inode, void *data)
  425. {
  426. return 0;
  427. }
  428. static int v9fs_set_inode(struct inode *inode, void *data)
  429. {
  430. struct v9fs_inode *v9inode = V9FS_I(inode);
  431. struct p9_wstat *st = (struct p9_wstat *)data;
  432. memcpy(&v9inode->qid, &st->qid, sizeof(st->qid));
  433. return 0;
  434. }
  435. static struct inode *v9fs_qid_iget(struct super_block *sb,
  436. struct p9_qid *qid,
  437. struct p9_wstat *st,
  438. int new)
  439. {
  440. dev_t rdev;
  441. int retval;
  442. umode_t umode;
  443. unsigned long i_ino;
  444. struct inode *inode;
  445. struct v9fs_session_info *v9ses = sb->s_fs_info;
  446. int (*test)(struct inode *, void *);
  447. if (new)
  448. test = v9fs_test_new_inode;
  449. else
  450. test = v9fs_test_inode;
  451. i_ino = v9fs_qid2ino(qid);
  452. inode = iget5_locked(sb, i_ino, test, v9fs_set_inode, st);
  453. if (!inode)
  454. return ERR_PTR(-ENOMEM);
  455. if (!(inode->i_state & I_NEW))
  456. return inode;
  457. /*
  458. * initialize the inode with the stat info
  459. * FIXME!! we may need support for stale inodes
  460. * later.
  461. */
  462. inode->i_ino = i_ino;
  463. umode = p9mode2unixmode(v9ses, st, &rdev);
  464. retval = v9fs_init_inode(v9ses, inode, umode, rdev);
  465. if (retval)
  466. goto error;
  467. v9fs_stat2inode(st, inode, sb);
  468. #ifdef CONFIG_9P_FSCACHE
  469. v9fs_cache_inode_get_cookie(inode);
  470. #endif
  471. unlock_new_inode(inode);
  472. return inode;
  473. error:
  474. iget_failed(inode);
  475. return ERR_PTR(retval);
  476. }
  477. struct inode *
  478. v9fs_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
  479. struct super_block *sb, int new)
  480. {
  481. struct p9_wstat *st;
  482. struct inode *inode = NULL;
  483. st = p9_client_stat(fid);
  484. if (IS_ERR(st))
  485. return ERR_CAST(st);
  486. inode = v9fs_qid_iget(sb, &st->qid, st, new);
  487. p9stat_free(st);
  488. kfree(st);
  489. return inode;
  490. }
  491. /**
  492. * v9fs_at_to_dotl_flags- convert Linux specific AT flags to
  493. * plan 9 AT flag.
  494. * @flags: flags to convert
  495. */
  496. static int v9fs_at_to_dotl_flags(int flags)
  497. {
  498. int rflags = 0;
  499. if (flags & AT_REMOVEDIR)
  500. rflags |= P9_DOTL_AT_REMOVEDIR;
  501. return rflags;
  502. }
  503. /**
  504. * v9fs_remove - helper function to remove files and directories
  505. * @dir: directory inode that is being deleted
  506. * @dentry: dentry that is being deleted
  507. * @rmdir: removing a directory
  508. *
  509. */
  510. static int v9fs_remove(struct inode *dir, struct dentry *dentry, int flags)
  511. {
  512. struct inode *inode;
  513. int retval = -EOPNOTSUPP;
  514. struct p9_fid *v9fid, *dfid;
  515. struct v9fs_session_info *v9ses;
  516. p9_debug(P9_DEBUG_VFS, "inode: %p dentry: %p rmdir: %x\n",
  517. dir, dentry, flags);
  518. v9ses = v9fs_inode2v9ses(dir);
  519. inode = dentry->d_inode;
  520. dfid = v9fs_fid_lookup(dentry->d_parent);
  521. if (IS_ERR(dfid)) {
  522. retval = PTR_ERR(dfid);
  523. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", retval);
  524. return retval;
  525. }
  526. if (v9fs_proto_dotl(v9ses))
  527. retval = p9_client_unlinkat(dfid, dentry->d_name.name,
  528. v9fs_at_to_dotl_flags(flags));
  529. if (retval == -EOPNOTSUPP) {
  530. /* Try the one based on path */
  531. v9fid = v9fs_fid_clone(dentry);
  532. if (IS_ERR(v9fid))
  533. return PTR_ERR(v9fid);
  534. retval = p9_client_remove(v9fid);
  535. }
  536. if (!retval) {
  537. /*
  538. * directories on unlink should have zero
  539. * link count
  540. */
  541. if (flags & AT_REMOVEDIR) {
  542. clear_nlink(inode);
  543. drop_nlink(dir);
  544. } else
  545. drop_nlink(inode);
  546. v9fs_invalidate_inode_attr(inode);
  547. v9fs_invalidate_inode_attr(dir);
  548. }
  549. return retval;
  550. }
  551. /**
  552. * v9fs_create - Create a file
  553. * @v9ses: session information
  554. * @dir: directory that dentry is being created in
  555. * @dentry: dentry that is being created
  556. * @extension: 9p2000.u extension string to support devices, etc.
  557. * @perm: create permissions
  558. * @mode: open mode
  559. *
  560. */
  561. static struct p9_fid *
  562. v9fs_create(struct v9fs_session_info *v9ses, struct inode *dir,
  563. struct dentry *dentry, char *extension, u32 perm, u8 mode)
  564. {
  565. int err;
  566. char *name;
  567. struct p9_fid *dfid, *ofid, *fid;
  568. struct inode *inode;
  569. p9_debug(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  570. err = 0;
  571. ofid = NULL;
  572. fid = NULL;
  573. name = (char *) dentry->d_name.name;
  574. dfid = v9fs_fid_lookup(dentry->d_parent);
  575. if (IS_ERR(dfid)) {
  576. err = PTR_ERR(dfid);
  577. p9_debug(P9_DEBUG_VFS, "fid lookup failed %d\n", err);
  578. return ERR_PTR(err);
  579. }
  580. /* clone a fid to use for creation */
  581. ofid = p9_client_walk(dfid, 0, NULL, 1);
  582. if (IS_ERR(ofid)) {
  583. err = PTR_ERR(ofid);
  584. p9_debug(P9_DEBUG_VFS, "p9_client_walk failed %d\n", err);
  585. return ERR_PTR(err);
  586. }
  587. err = p9_client_fcreate(ofid, name, perm, mode, extension);
  588. if (err < 0) {
  589. p9_debug(P9_DEBUG_VFS, "p9_client_fcreate failed %d\n", err);
  590. goto error;
  591. }
  592. if (!(perm & P9_DMLINK)) {
  593. /* now walk from the parent so we can get unopened fid */
  594. fid = p9_client_walk(dfid, 1, &name, 1);
  595. if (IS_ERR(fid)) {
  596. err = PTR_ERR(fid);
  597. p9_debug(P9_DEBUG_VFS,
  598. "p9_client_walk failed %d\n", err);
  599. fid = NULL;
  600. goto error;
  601. }
  602. /*
  603. * instantiate inode and assign the unopened fid to the dentry
  604. */
  605. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  606. if (IS_ERR(inode)) {
  607. err = PTR_ERR(inode);
  608. p9_debug(P9_DEBUG_VFS,
  609. "inode creation failed %d\n", err);
  610. goto error;
  611. }
  612. err = v9fs_fid_add(dentry, fid);
  613. if (err < 0)
  614. goto error;
  615. d_instantiate(dentry, inode);
  616. }
  617. return ofid;
  618. error:
  619. if (ofid)
  620. p9_client_clunk(ofid);
  621. if (fid)
  622. p9_client_clunk(fid);
  623. return ERR_PTR(err);
  624. }
  625. /**
  626. * v9fs_vfs_create - VFS hook to create files
  627. * @dir: directory inode that is being created
  628. * @dentry: dentry that is being deleted
  629. * @mode: create permissions
  630. * @nd: path information
  631. *
  632. */
  633. static int
  634. v9fs_vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  635. struct nameidata *nd)
  636. {
  637. int err;
  638. u32 perm;
  639. int flags;
  640. struct file *filp;
  641. struct v9fs_inode *v9inode;
  642. struct v9fs_session_info *v9ses;
  643. struct p9_fid *fid, *inode_fid;
  644. err = 0;
  645. fid = NULL;
  646. v9ses = v9fs_inode2v9ses(dir);
  647. perm = unixmode2p9mode(v9ses, mode);
  648. if (nd)
  649. flags = nd->intent.open.flags;
  650. else
  651. flags = O_RDWR;
  652. fid = v9fs_create(v9ses, dir, dentry, NULL, perm,
  653. v9fs_uflags2omode(flags,
  654. v9fs_proto_dotu(v9ses)));
  655. if (IS_ERR(fid)) {
  656. err = PTR_ERR(fid);
  657. fid = NULL;
  658. goto error;
  659. }
  660. v9fs_invalidate_inode_attr(dir);
  661. /* if we are opening a file, assign the open fid to the file */
  662. if (nd) {
  663. v9inode = V9FS_I(dentry->d_inode);
  664. mutex_lock(&v9inode->v_mutex);
  665. if (v9ses->cache && !v9inode->writeback_fid &&
  666. ((flags & O_ACCMODE) != O_RDONLY)) {
  667. /*
  668. * clone a fid and add it to writeback_fid
  669. * we do it during open time instead of
  670. * page dirty time via write_begin/page_mkwrite
  671. * because we want write after unlink usecase
  672. * to work.
  673. */
  674. inode_fid = v9fs_writeback_fid(dentry);
  675. if (IS_ERR(inode_fid)) {
  676. err = PTR_ERR(inode_fid);
  677. mutex_unlock(&v9inode->v_mutex);
  678. goto error;
  679. }
  680. v9inode->writeback_fid = (void *) inode_fid;
  681. }
  682. mutex_unlock(&v9inode->v_mutex);
  683. filp = lookup_instantiate_filp(nd, dentry, generic_file_open);
  684. if (IS_ERR(filp)) {
  685. err = PTR_ERR(filp);
  686. goto error;
  687. }
  688. filp->private_data = fid;
  689. #ifdef CONFIG_9P_FSCACHE
  690. if (v9ses->cache)
  691. v9fs_cache_inode_set_cookie(dentry->d_inode, filp);
  692. #endif
  693. } else
  694. p9_client_clunk(fid);
  695. return 0;
  696. error:
  697. if (fid)
  698. p9_client_clunk(fid);
  699. return err;
  700. }
  701. /**
  702. * v9fs_vfs_mkdir - VFS mkdir hook to create a directory
  703. * @dir: inode that is being unlinked
  704. * @dentry: dentry that is being unlinked
  705. * @mode: mode for new directory
  706. *
  707. */
  708. static int v9fs_vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  709. {
  710. int err;
  711. u32 perm;
  712. struct p9_fid *fid;
  713. struct v9fs_session_info *v9ses;
  714. p9_debug(P9_DEBUG_VFS, "name %s\n", dentry->d_name.name);
  715. err = 0;
  716. v9ses = v9fs_inode2v9ses(dir);
  717. perm = unixmode2p9mode(v9ses, mode | S_IFDIR);
  718. fid = v9fs_create(v9ses, dir, dentry, NULL, perm, P9_OREAD);
  719. if (IS_ERR(fid)) {
  720. err = PTR_ERR(fid);
  721. fid = NULL;
  722. } else {
  723. inc_nlink(dir);
  724. v9fs_invalidate_inode_attr(dir);
  725. }
  726. if (fid)
  727. p9_client_clunk(fid);
  728. return err;
  729. }
  730. /**
  731. * v9fs_vfs_lookup - VFS lookup hook to "walk" to a new inode
  732. * @dir: inode that is being walked from
  733. * @dentry: dentry that is being walked to?
  734. * @nameidata: path data
  735. *
  736. */
  737. struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
  738. struct nameidata *nameidata)
  739. {
  740. struct dentry *res;
  741. struct super_block *sb;
  742. struct v9fs_session_info *v9ses;
  743. struct p9_fid *dfid, *fid;
  744. struct inode *inode;
  745. char *name;
  746. int result = 0;
  747. p9_debug(P9_DEBUG_VFS, "dir: %p dentry: (%s) %p nameidata: %p\n",
  748. dir, dentry->d_name.name, dentry, nameidata);
  749. if (dentry->d_name.len > NAME_MAX)
  750. return ERR_PTR(-ENAMETOOLONG);
  751. sb = dir->i_sb;
  752. v9ses = v9fs_inode2v9ses(dir);
  753. /* We can walk d_parent because we hold the dir->i_mutex */
  754. dfid = v9fs_fid_lookup(dentry->d_parent);
  755. if (IS_ERR(dfid))
  756. return ERR_CAST(dfid);
  757. name = (char *) dentry->d_name.name;
  758. fid = p9_client_walk(dfid, 1, &name, 1);
  759. if (IS_ERR(fid)) {
  760. result = PTR_ERR(fid);
  761. if (result == -ENOENT) {
  762. inode = NULL;
  763. goto inst_out;
  764. }
  765. return ERR_PTR(result);
  766. }
  767. /*
  768. * Make sure we don't use a wrong inode due to parallel
  769. * unlink. For cached mode create calls request for new
  770. * inode. But with cache disabled, lookup should do this.
  771. */
  772. if (v9ses->cache)
  773. inode = v9fs_get_inode_from_fid(v9ses, fid, dir->i_sb);
  774. else
  775. inode = v9fs_get_new_inode_from_fid(v9ses, fid, dir->i_sb);
  776. if (IS_ERR(inode)) {
  777. result = PTR_ERR(inode);
  778. inode = NULL;
  779. goto error;
  780. }
  781. result = v9fs_fid_add(dentry, fid);
  782. if (result < 0)
  783. goto error_iput;
  784. inst_out:
  785. /*
  786. * If we had a rename on the server and a parallel lookup
  787. * for the new name, then make sure we instantiate with
  788. * the new name. ie look up for a/b, while on server somebody
  789. * moved b under k and client parallely did a lookup for
  790. * k/b.
  791. */
  792. res = d_materialise_unique(dentry, inode);
  793. if (!IS_ERR(res))
  794. return res;
  795. result = PTR_ERR(res);
  796. error_iput:
  797. iput(inode);
  798. error:
  799. p9_client_clunk(fid);
  800. return ERR_PTR(result);
  801. }
  802. /**
  803. * v9fs_vfs_unlink - VFS unlink hook to delete an inode
  804. * @i: inode that is being unlinked
  805. * @d: dentry that is being unlinked
  806. *
  807. */
  808. int v9fs_vfs_unlink(struct inode *i, struct dentry *d)
  809. {
  810. return v9fs_remove(i, d, 0);
  811. }
  812. /**
  813. * v9fs_vfs_rmdir - VFS unlink hook to delete a directory
  814. * @i: inode that is being unlinked
  815. * @d: dentry that is being unlinked
  816. *
  817. */
  818. int v9fs_vfs_rmdir(struct inode *i, struct dentry *d)
  819. {
  820. return v9fs_remove(i, d, AT_REMOVEDIR);
  821. }
  822. /**
  823. * v9fs_vfs_rename - VFS hook to rename an inode
  824. * @old_dir: old dir inode
  825. * @old_dentry: old dentry
  826. * @new_dir: new dir inode
  827. * @new_dentry: new dentry
  828. *
  829. */
  830. int
  831. v9fs_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  832. struct inode *new_dir, struct dentry *new_dentry)
  833. {
  834. int retval;
  835. struct inode *old_inode;
  836. struct inode *new_inode;
  837. struct v9fs_session_info *v9ses;
  838. struct p9_fid *oldfid;
  839. struct p9_fid *olddirfid;
  840. struct p9_fid *newdirfid;
  841. struct p9_wstat wstat;
  842. p9_debug(P9_DEBUG_VFS, "\n");
  843. retval = 0;
  844. old_inode = old_dentry->d_inode;
  845. new_inode = new_dentry->d_inode;
  846. v9ses = v9fs_inode2v9ses(old_inode);
  847. oldfid = v9fs_fid_lookup(old_dentry);
  848. if (IS_ERR(oldfid))
  849. return PTR_ERR(oldfid);
  850. olddirfid = v9fs_fid_clone(old_dentry->d_parent);
  851. if (IS_ERR(olddirfid)) {
  852. retval = PTR_ERR(olddirfid);
  853. goto done;
  854. }
  855. newdirfid = v9fs_fid_clone(new_dentry->d_parent);
  856. if (IS_ERR(newdirfid)) {
  857. retval = PTR_ERR(newdirfid);
  858. goto clunk_olddir;
  859. }
  860. down_write(&v9ses->rename_sem);
  861. if (v9fs_proto_dotl(v9ses)) {
  862. retval = p9_client_renameat(olddirfid, old_dentry->d_name.name,
  863. newdirfid, new_dentry->d_name.name);
  864. if (retval == -EOPNOTSUPP)
  865. retval = p9_client_rename(oldfid, newdirfid,
  866. new_dentry->d_name.name);
  867. if (retval != -EOPNOTSUPP)
  868. goto clunk_newdir;
  869. }
  870. if (old_dentry->d_parent != new_dentry->d_parent) {
  871. /*
  872. * 9P .u can only handle file rename in the same directory
  873. */
  874. p9_debug(P9_DEBUG_ERROR, "old dir and new dir are different\n");
  875. retval = -EXDEV;
  876. goto clunk_newdir;
  877. }
  878. v9fs_blank_wstat(&wstat);
  879. wstat.muid = v9ses->uname;
  880. wstat.name = (char *) new_dentry->d_name.name;
  881. retval = p9_client_wstat(oldfid, &wstat);
  882. clunk_newdir:
  883. if (!retval) {
  884. if (new_inode) {
  885. if (S_ISDIR(new_inode->i_mode))
  886. clear_nlink(new_inode);
  887. else
  888. drop_nlink(new_inode);
  889. }
  890. if (S_ISDIR(old_inode->i_mode)) {
  891. if (!new_inode)
  892. inc_nlink(new_dir);
  893. drop_nlink(old_dir);
  894. }
  895. v9fs_invalidate_inode_attr(old_inode);
  896. v9fs_invalidate_inode_attr(old_dir);
  897. v9fs_invalidate_inode_attr(new_dir);
  898. /* successful rename */
  899. d_move(old_dentry, new_dentry);
  900. }
  901. up_write(&v9ses->rename_sem);
  902. p9_client_clunk(newdirfid);
  903. clunk_olddir:
  904. p9_client_clunk(olddirfid);
  905. done:
  906. return retval;
  907. }
  908. /**
  909. * v9fs_vfs_getattr - retrieve file metadata
  910. * @mnt: mount information
  911. * @dentry: file to get attributes on
  912. * @stat: metadata structure to populate
  913. *
  914. */
  915. static int
  916. v9fs_vfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  917. struct kstat *stat)
  918. {
  919. int err;
  920. struct v9fs_session_info *v9ses;
  921. struct p9_fid *fid;
  922. struct p9_wstat *st;
  923. p9_debug(P9_DEBUG_VFS, "dentry: %p\n", dentry);
  924. err = -EPERM;
  925. v9ses = v9fs_dentry2v9ses(dentry);
  926. if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) {
  927. generic_fillattr(dentry->d_inode, stat);
  928. return 0;
  929. }
  930. fid = v9fs_fid_lookup(dentry);
  931. if (IS_ERR(fid))
  932. return PTR_ERR(fid);
  933. st = p9_client_stat(fid);
  934. if (IS_ERR(st))
  935. return PTR_ERR(st);
  936. v9fs_stat2inode(st, dentry->d_inode, dentry->d_inode->i_sb);
  937. generic_fillattr(dentry->d_inode, stat);
  938. p9stat_free(st);
  939. kfree(st);
  940. return 0;
  941. }
  942. /**
  943. * v9fs_vfs_setattr - set file metadata
  944. * @dentry: file whose metadata to set
  945. * @iattr: metadata assignment structure
  946. *
  947. */
  948. static int v9fs_vfs_setattr(struct dentry *dentry, struct iattr *iattr)
  949. {
  950. int retval;
  951. struct v9fs_session_info *v9ses;
  952. struct p9_fid *fid;
  953. struct p9_wstat wstat;
  954. p9_debug(P9_DEBUG_VFS, "\n");
  955. retval = inode_change_ok(dentry->d_inode, iattr);
  956. if (retval)
  957. return retval;
  958. retval = -EPERM;
  959. v9ses = v9fs_dentry2v9ses(dentry);
  960. fid = v9fs_fid_lookup(dentry);
  961. if(IS_ERR(fid))
  962. return PTR_ERR(fid);
  963. v9fs_blank_wstat(&wstat);
  964. if (iattr->ia_valid & ATTR_MODE)
  965. wstat.mode = unixmode2p9mode(v9ses, iattr->ia_mode);
  966. if (iattr->ia_valid & ATTR_MTIME)
  967. wstat.mtime = iattr->ia_mtime.tv_sec;
  968. if (iattr->ia_valid & ATTR_ATIME)
  969. wstat.atime = iattr->ia_atime.tv_sec;
  970. if (iattr->ia_valid & ATTR_SIZE)
  971. wstat.length = iattr->ia_size;
  972. if (v9fs_proto_dotu(v9ses)) {
  973. if (iattr->ia_valid & ATTR_UID)
  974. wstat.n_uid = iattr->ia_uid;
  975. if (iattr->ia_valid & ATTR_GID)
  976. wstat.n_gid = iattr->ia_gid;
  977. }
  978. /* Write all dirty data */
  979. if (S_ISREG(dentry->d_inode->i_mode))
  980. filemap_write_and_wait(dentry->d_inode->i_mapping);
  981. retval = p9_client_wstat(fid, &wstat);
  982. if (retval < 0)
  983. return retval;
  984. if ((iattr->ia_valid & ATTR_SIZE) &&
  985. iattr->ia_size != i_size_read(dentry->d_inode))
  986. truncate_setsize(dentry->d_inode, iattr->ia_size);
  987. v9fs_invalidate_inode_attr(dentry->d_inode);
  988. setattr_copy(dentry->d_inode, iattr);
  989. mark_inode_dirty(dentry->d_inode);
  990. return 0;
  991. }
  992. /**
  993. * v9fs_stat2inode - populate an inode structure with mistat info
  994. * @stat: Plan 9 metadata (mistat) structure
  995. * @inode: inode to populate
  996. * @sb: superblock of filesystem
  997. *
  998. */
  999. void
  1000. v9fs_stat2inode(struct p9_wstat *stat, struct inode *inode,
  1001. struct super_block *sb)
  1002. {
  1003. umode_t mode;
  1004. char ext[32];
  1005. char tag_name[14];
  1006. unsigned int i_nlink;
  1007. struct v9fs_session_info *v9ses = sb->s_fs_info;
  1008. struct v9fs_inode *v9inode = V9FS_I(inode);
  1009. set_nlink(inode, 1);
  1010. inode->i_atime.tv_sec = stat->atime;
  1011. inode->i_mtime.tv_sec = stat->mtime;
  1012. inode->i_ctime.tv_sec = stat->mtime;
  1013. inode->i_uid = v9ses->dfltuid;
  1014. inode->i_gid = v9ses->dfltgid;
  1015. if (v9fs_proto_dotu(v9ses)) {
  1016. inode->i_uid = stat->n_uid;
  1017. inode->i_gid = stat->n_gid;
  1018. }
  1019. if ((S_ISREG(inode->i_mode)) || (S_ISDIR(inode->i_mode))) {
  1020. if (v9fs_proto_dotu(v9ses) && (stat->extension[0] != '\0')) {
  1021. /*
  1022. * Hadlink support got added later to
  1023. * to the .u extension. So there can be
  1024. * server out there that doesn't support
  1025. * this even with .u extension. So check
  1026. * for non NULL stat->extension
  1027. */
  1028. strncpy(ext, stat->extension, sizeof(ext));
  1029. /* HARDLINKCOUNT %u */
  1030. sscanf(ext, "%13s %u", tag_name, &i_nlink);
  1031. if (!strncmp(tag_name, "HARDLINKCOUNT", 13))
  1032. set_nlink(inode, i_nlink);
  1033. }
  1034. }
  1035. mode = p9mode2perm(v9ses, stat);
  1036. mode |= inode->i_mode & ~S_IALLUGO;
  1037. inode->i_mode = mode;
  1038. i_size_write(inode, stat->length);
  1039. /* not real number of blocks, but 512 byte ones ... */
  1040. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  1041. v9inode->cache_validity &= ~V9FS_INO_INVALID_ATTR;
  1042. }
  1043. /**
  1044. * v9fs_qid2ino - convert qid into inode number
  1045. * @qid: qid to hash
  1046. *
  1047. * BUG: potential for inode number collisions?
  1048. */
  1049. ino_t v9fs_qid2ino(struct p9_qid *qid)
  1050. {
  1051. u64 path = qid->path + 2;
  1052. ino_t i = 0;
  1053. if (sizeof(ino_t) == sizeof(path))
  1054. memcpy(&i, &path, sizeof(ino_t));
  1055. else
  1056. i = (ino_t) (path ^ (path >> 32));
  1057. return i;
  1058. }
  1059. /**
  1060. * v9fs_readlink - read a symlink's location (internal version)
  1061. * @dentry: dentry for symlink
  1062. * @buffer: buffer to load symlink location into
  1063. * @buflen: length of buffer
  1064. *
  1065. */
  1066. static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
  1067. {
  1068. int retval;
  1069. struct v9fs_session_info *v9ses;
  1070. struct p9_fid *fid;
  1071. struct p9_wstat *st;
  1072. p9_debug(P9_DEBUG_VFS, " %s\n", dentry->d_name.name);
  1073. retval = -EPERM;
  1074. v9ses = v9fs_dentry2v9ses(dentry);
  1075. fid = v9fs_fid_lookup(dentry);
  1076. if (IS_ERR(fid))
  1077. return PTR_ERR(fid);
  1078. if (!v9fs_proto_dotu(v9ses))
  1079. return -EBADF;
  1080. st = p9_client_stat(fid);
  1081. if (IS_ERR(st))
  1082. return PTR_ERR(st);
  1083. if (!(st->mode & P9_DMSYMLINK)) {
  1084. retval = -EINVAL;
  1085. goto done;
  1086. }
  1087. /* copy extension buffer into buffer */
  1088. strncpy(buffer, st->extension, buflen);
  1089. p9_debug(P9_DEBUG_VFS, "%s -> %s (%s)\n",
  1090. dentry->d_name.name, st->extension, buffer);
  1091. retval = strnlen(buffer, buflen);
  1092. done:
  1093. p9stat_free(st);
  1094. kfree(st);
  1095. return retval;
  1096. }
  1097. /**
  1098. * v9fs_vfs_follow_link - follow a symlink path
  1099. * @dentry: dentry for symlink
  1100. * @nd: nameidata
  1101. *
  1102. */
  1103. static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  1104. {
  1105. int len = 0;
  1106. char *link = __getname();
  1107. p9_debug(P9_DEBUG_VFS, "%s\n", dentry->d_name.name);
  1108. if (!link)
  1109. link = ERR_PTR(-ENOMEM);
  1110. else {
  1111. len = v9fs_readlink(dentry, link, PATH_MAX);
  1112. if (len < 0) {
  1113. __putname(link);
  1114. link = ERR_PTR(len);
  1115. } else
  1116. link[min(len, PATH_MAX-1)] = 0;
  1117. }
  1118. nd_set_link(nd, link);
  1119. return NULL;
  1120. }
  1121. /**
  1122. * v9fs_vfs_put_link - release a symlink path
  1123. * @dentry: dentry for symlink
  1124. * @nd: nameidata
  1125. * @p: unused
  1126. *
  1127. */
  1128. void
  1129. v9fs_vfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
  1130. {
  1131. char *s = nd_get_link(nd);
  1132. p9_debug(P9_DEBUG_VFS, " %s %s\n",
  1133. dentry->d_name.name, IS_ERR(s) ? "<error>" : s);
  1134. if (!IS_ERR(s))
  1135. __putname(s);
  1136. }
  1137. /**
  1138. * v9fs_vfs_mkspecial - create a special file
  1139. * @dir: inode to create special file in
  1140. * @dentry: dentry to create
  1141. * @mode: mode to create special file
  1142. * @extension: 9p2000.u format extension string representing special file
  1143. *
  1144. */
  1145. static int v9fs_vfs_mkspecial(struct inode *dir, struct dentry *dentry,
  1146. u32 perm, const char *extension)
  1147. {
  1148. struct p9_fid *fid;
  1149. struct v9fs_session_info *v9ses;
  1150. v9ses = v9fs_inode2v9ses(dir);
  1151. if (!v9fs_proto_dotu(v9ses)) {
  1152. p9_debug(P9_DEBUG_ERROR, "not extended\n");
  1153. return -EPERM;
  1154. }
  1155. fid = v9fs_create(v9ses, dir, dentry, (char *) extension, perm,
  1156. P9_OREAD);
  1157. if (IS_ERR(fid))
  1158. return PTR_ERR(fid);
  1159. v9fs_invalidate_inode_attr(dir);
  1160. p9_client_clunk(fid);
  1161. return 0;
  1162. }
  1163. /**
  1164. * v9fs_vfs_symlink - helper function to create symlinks
  1165. * @dir: directory inode containing symlink
  1166. * @dentry: dentry for symlink
  1167. * @symname: symlink data
  1168. *
  1169. * See Also: 9P2000.u RFC for more information
  1170. *
  1171. */
  1172. static int
  1173. v9fs_vfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1174. {
  1175. p9_debug(P9_DEBUG_VFS, " %lu,%s,%s\n",
  1176. dir->i_ino, dentry->d_name.name, symname);
  1177. return v9fs_vfs_mkspecial(dir, dentry, P9_DMSYMLINK, symname);
  1178. }
  1179. /**
  1180. * v9fs_vfs_link - create a hardlink
  1181. * @old_dentry: dentry for file to link to
  1182. * @dir: inode destination for new link
  1183. * @dentry: dentry for link
  1184. *
  1185. */
  1186. static int
  1187. v9fs_vfs_link(struct dentry *old_dentry, struct inode *dir,
  1188. struct dentry *dentry)
  1189. {
  1190. int retval;
  1191. char *name;
  1192. struct p9_fid *oldfid;
  1193. p9_debug(P9_DEBUG_VFS, " %lu,%s,%s\n",
  1194. dir->i_ino, dentry->d_name.name, old_dentry->d_name.name);
  1195. oldfid = v9fs_fid_clone(old_dentry);
  1196. if (IS_ERR(oldfid))
  1197. return PTR_ERR(oldfid);
  1198. name = __getname();
  1199. if (unlikely(!name)) {
  1200. retval = -ENOMEM;
  1201. goto clunk_fid;
  1202. }
  1203. sprintf(name, "%d\n", oldfid->fid);
  1204. retval = v9fs_vfs_mkspecial(dir, dentry, P9_DMLINK, name);
  1205. __putname(name);
  1206. if (!retval) {
  1207. v9fs_refresh_inode(oldfid, old_dentry->d_inode);
  1208. v9fs_invalidate_inode_attr(dir);
  1209. }
  1210. clunk_fid:
  1211. p9_client_clunk(oldfid);
  1212. return retval;
  1213. }
  1214. /**
  1215. * v9fs_vfs_mknod - create a special file
  1216. * @dir: inode destination for new link
  1217. * @dentry: dentry for file
  1218. * @mode: mode for creation
  1219. * @rdev: device associated with special file
  1220. *
  1221. */
  1222. static int
  1223. v9fs_vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  1224. {
  1225. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dir);
  1226. int retval;
  1227. char *name;
  1228. u32 perm;
  1229. p9_debug(P9_DEBUG_VFS, " %lu,%s mode: %hx MAJOR: %u MINOR: %u\n",
  1230. dir->i_ino, dentry->d_name.name, mode,
  1231. MAJOR(rdev), MINOR(rdev));
  1232. if (!new_valid_dev(rdev))
  1233. return -EINVAL;
  1234. name = __getname();
  1235. if (!name)
  1236. return -ENOMEM;
  1237. /* build extension */
  1238. if (S_ISBLK(mode))
  1239. sprintf(name, "b %u %u", MAJOR(rdev), MINOR(rdev));
  1240. else if (S_ISCHR(mode))
  1241. sprintf(name, "c %u %u", MAJOR(rdev), MINOR(rdev));
  1242. else if (S_ISFIFO(mode))
  1243. *name = 0;
  1244. else if (S_ISSOCK(mode))
  1245. *name = 0;
  1246. else {
  1247. __putname(name);
  1248. return -EINVAL;
  1249. }
  1250. perm = unixmode2p9mode(v9ses, mode);
  1251. retval = v9fs_vfs_mkspecial(dir, dentry, perm, name);
  1252. __putname(name);
  1253. return retval;
  1254. }
  1255. int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode)
  1256. {
  1257. int umode;
  1258. dev_t rdev;
  1259. loff_t i_size;
  1260. struct p9_wstat *st;
  1261. struct v9fs_session_info *v9ses;
  1262. v9ses = v9fs_inode2v9ses(inode);
  1263. st = p9_client_stat(fid);
  1264. if (IS_ERR(st))
  1265. return PTR_ERR(st);
  1266. /*
  1267. * Don't update inode if the file type is different
  1268. */
  1269. umode = p9mode2unixmode(v9ses, st, &rdev);
  1270. if ((inode->i_mode & S_IFMT) != (umode & S_IFMT))
  1271. goto out;
  1272. spin_lock(&inode->i_lock);
  1273. /*
  1274. * We don't want to refresh inode->i_size,
  1275. * because we may have cached data
  1276. */
  1277. i_size = inode->i_size;
  1278. v9fs_stat2inode(st, inode, inode->i_sb);
  1279. if (v9ses->cache)
  1280. inode->i_size = i_size;
  1281. spin_unlock(&inode->i_lock);
  1282. out:
  1283. p9stat_free(st);
  1284. kfree(st);
  1285. return 0;
  1286. }
  1287. static const struct inode_operations v9fs_dir_inode_operations_dotu = {
  1288. .create = v9fs_vfs_create,
  1289. .lookup = v9fs_vfs_lookup,
  1290. .symlink = v9fs_vfs_symlink,
  1291. .link = v9fs_vfs_link,
  1292. .unlink = v9fs_vfs_unlink,
  1293. .mkdir = v9fs_vfs_mkdir,
  1294. .rmdir = v9fs_vfs_rmdir,
  1295. .mknod = v9fs_vfs_mknod,
  1296. .rename = v9fs_vfs_rename,
  1297. .getattr = v9fs_vfs_getattr,
  1298. .setattr = v9fs_vfs_setattr,
  1299. };
  1300. static const struct inode_operations v9fs_dir_inode_operations = {
  1301. .create = v9fs_vfs_create,
  1302. .lookup = v9fs_vfs_lookup,
  1303. .unlink = v9fs_vfs_unlink,
  1304. .mkdir = v9fs_vfs_mkdir,
  1305. .rmdir = v9fs_vfs_rmdir,
  1306. .mknod = v9fs_vfs_mknod,
  1307. .rename = v9fs_vfs_rename,
  1308. .getattr = v9fs_vfs_getattr,
  1309. .setattr = v9fs_vfs_setattr,
  1310. };
  1311. static const struct inode_operations v9fs_file_inode_operations = {
  1312. .getattr = v9fs_vfs_getattr,
  1313. .setattr = v9fs_vfs_setattr,
  1314. };
  1315. static const struct inode_operations v9fs_symlink_inode_operations = {
  1316. .readlink = generic_readlink,
  1317. .follow_link = v9fs_vfs_follow_link,
  1318. .put_link = v9fs_vfs_put_link,
  1319. .getattr = v9fs_vfs_getattr,
  1320. .setattr = v9fs_vfs_setattr,
  1321. };