vfs_inode.c 31 KB

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