dir.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. /*
  2. * linux/fs/nfs/dir.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs directory handling functions
  7. *
  8. * 10 Apr 1996 Added silly rename for unlink --okir
  9. * 28 Sep 1996 Improved directory cache --okir
  10. * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
  11. * Re-implemented silly rename for unlink, newly implemented
  12. * silly rename for nfs_rename() following the suggestions
  13. * of Olaf Kirch (okir) found in this file.
  14. * Following Linus comments on my original hack, this version
  15. * depends only on the dcache stuff and doesn't touch the inode
  16. * layer (iput() and friends).
  17. * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
  18. */
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/stat.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/string.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/pagevec.h>
  33. #include <linux/namei.h>
  34. #include <linux/mount.h>
  35. #include <linux/sched.h>
  36. #include "nfs4_fs.h"
  37. #include "delegation.h"
  38. #include "iostat.h"
  39. #include "internal.h"
  40. /* #define NFS_DEBUG_VERBOSE 1 */
  41. static int nfs_opendir(struct inode *, struct file *);
  42. static int nfs_readdir(struct file *, void *, filldir_t);
  43. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  44. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  45. static int nfs_mkdir(struct inode *, struct dentry *, int);
  46. static int nfs_rmdir(struct inode *, struct dentry *);
  47. static int nfs_unlink(struct inode *, struct dentry *);
  48. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  49. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  50. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  51. static int nfs_rename(struct inode *, struct dentry *,
  52. struct inode *, struct dentry *);
  53. static int nfs_fsync_dir(struct file *, struct dentry *, int);
  54. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  55. const struct file_operations nfs_dir_operations = {
  56. .llseek = nfs_llseek_dir,
  57. .read = generic_read_dir,
  58. .readdir = nfs_readdir,
  59. .open = nfs_opendir,
  60. .release = nfs_release,
  61. .fsync = nfs_fsync_dir,
  62. };
  63. const struct inode_operations nfs_dir_inode_operations = {
  64. .create = nfs_create,
  65. .lookup = nfs_lookup,
  66. .link = nfs_link,
  67. .unlink = nfs_unlink,
  68. .symlink = nfs_symlink,
  69. .mkdir = nfs_mkdir,
  70. .rmdir = nfs_rmdir,
  71. .mknod = nfs_mknod,
  72. .rename = nfs_rename,
  73. .permission = nfs_permission,
  74. .getattr = nfs_getattr,
  75. .setattr = nfs_setattr,
  76. };
  77. #ifdef CONFIG_NFS_V3
  78. const struct inode_operations nfs3_dir_inode_operations = {
  79. .create = nfs_create,
  80. .lookup = nfs_lookup,
  81. .link = nfs_link,
  82. .unlink = nfs_unlink,
  83. .symlink = nfs_symlink,
  84. .mkdir = nfs_mkdir,
  85. .rmdir = nfs_rmdir,
  86. .mknod = nfs_mknod,
  87. .rename = nfs_rename,
  88. .permission = nfs_permission,
  89. .getattr = nfs_getattr,
  90. .setattr = nfs_setattr,
  91. .listxattr = nfs3_listxattr,
  92. .getxattr = nfs3_getxattr,
  93. .setxattr = nfs3_setxattr,
  94. .removexattr = nfs3_removexattr,
  95. };
  96. #endif /* CONFIG_NFS_V3 */
  97. #ifdef CONFIG_NFS_V4
  98. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  99. const struct inode_operations nfs4_dir_inode_operations = {
  100. .create = nfs_create,
  101. .lookup = nfs_atomic_lookup,
  102. .link = nfs_link,
  103. .unlink = nfs_unlink,
  104. .symlink = nfs_symlink,
  105. .mkdir = nfs_mkdir,
  106. .rmdir = nfs_rmdir,
  107. .mknod = nfs_mknod,
  108. .rename = nfs_rename,
  109. .permission = nfs_permission,
  110. .getattr = nfs_getattr,
  111. .setattr = nfs_setattr,
  112. .getxattr = nfs4_getxattr,
  113. .setxattr = nfs4_setxattr,
  114. .listxattr = nfs4_listxattr,
  115. };
  116. #endif /* CONFIG_NFS_V4 */
  117. /*
  118. * Open file
  119. */
  120. static int
  121. nfs_opendir(struct inode *inode, struct file *filp)
  122. {
  123. int res;
  124. dfprintk(FILE, "NFS: open dir(%s/%s)\n",
  125. filp->f_path.dentry->d_parent->d_name.name,
  126. filp->f_path.dentry->d_name.name);
  127. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  128. /* Call generic open code in order to cache credentials */
  129. res = nfs_open(inode, filp);
  130. return res;
  131. }
  132. typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
  133. typedef struct {
  134. struct file *file;
  135. struct page *page;
  136. unsigned long page_index;
  137. __be32 *ptr;
  138. u64 *dir_cookie;
  139. loff_t current_index;
  140. struct nfs_entry *entry;
  141. decode_dirent_t decode;
  142. int plus;
  143. unsigned long timestamp;
  144. unsigned long gencount;
  145. int timestamp_valid;
  146. } nfs_readdir_descriptor_t;
  147. /* Now we cache directories properly, by stuffing the dirent
  148. * data directly in the page cache.
  149. *
  150. * Inode invalidation due to refresh etc. takes care of
  151. * _everything_, no sloppy entry flushing logic, no extraneous
  152. * copying, network direct to page cache, the way it was meant
  153. * to be.
  154. *
  155. * NOTE: Dirent information verification is done always by the
  156. * page-in of the RPC reply, nowhere else, this simplies
  157. * things substantially.
  158. */
  159. static
  160. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
  161. {
  162. struct file *file = desc->file;
  163. struct inode *inode = file->f_path.dentry->d_inode;
  164. struct rpc_cred *cred = nfs_file_cred(file);
  165. unsigned long timestamp, gencount;
  166. int error;
  167. dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
  168. __func__, (long long)desc->entry->cookie,
  169. page->index);
  170. again:
  171. timestamp = jiffies;
  172. gencount = nfs_inc_attr_generation_counter();
  173. error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
  174. NFS_SERVER(inode)->dtsize, desc->plus);
  175. if (error < 0) {
  176. /* We requested READDIRPLUS, but the server doesn't grok it */
  177. if (error == -ENOTSUPP && desc->plus) {
  178. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  179. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  180. desc->plus = 0;
  181. goto again;
  182. }
  183. goto error;
  184. }
  185. desc->timestamp = timestamp;
  186. desc->gencount = gencount;
  187. desc->timestamp_valid = 1;
  188. SetPageUptodate(page);
  189. /* Ensure consistent page alignment of the data.
  190. * Note: assumes we have exclusive access to this mapping either
  191. * through inode->i_mutex or some other mechanism.
  192. */
  193. if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
  194. /* Should never happen */
  195. nfs_zap_mapping(inode, inode->i_mapping);
  196. }
  197. unlock_page(page);
  198. return 0;
  199. error:
  200. unlock_page(page);
  201. return -EIO;
  202. }
  203. static inline
  204. int dir_decode(nfs_readdir_descriptor_t *desc)
  205. {
  206. __be32 *p = desc->ptr;
  207. p = desc->decode(p, desc->entry, desc->plus);
  208. if (IS_ERR(p))
  209. return PTR_ERR(p);
  210. desc->ptr = p;
  211. if (desc->timestamp_valid) {
  212. desc->entry->fattr->time_start = desc->timestamp;
  213. desc->entry->fattr->gencount = desc->gencount;
  214. } else
  215. desc->entry->fattr->valid &= ~NFS_ATTR_FATTR;
  216. return 0;
  217. }
  218. static inline
  219. void dir_page_release(nfs_readdir_descriptor_t *desc)
  220. {
  221. kunmap(desc->page);
  222. page_cache_release(desc->page);
  223. desc->page = NULL;
  224. desc->ptr = NULL;
  225. }
  226. /*
  227. * Given a pointer to a buffer that has already been filled by a call
  228. * to readdir, find the next entry with cookie '*desc->dir_cookie'.
  229. *
  230. * If the end of the buffer has been reached, return -EAGAIN, if not,
  231. * return the offset within the buffer of the next entry to be
  232. * read.
  233. */
  234. static inline
  235. int find_dirent(nfs_readdir_descriptor_t *desc)
  236. {
  237. struct nfs_entry *entry = desc->entry;
  238. int loop_count = 0,
  239. status;
  240. while((status = dir_decode(desc)) == 0) {
  241. dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
  242. __func__, (unsigned long long)entry->cookie);
  243. if (entry->prev_cookie == *desc->dir_cookie)
  244. break;
  245. if (loop_count++ > 200) {
  246. loop_count = 0;
  247. schedule();
  248. }
  249. }
  250. return status;
  251. }
  252. /*
  253. * Given a pointer to a buffer that has already been filled by a call
  254. * to readdir, find the entry at offset 'desc->file->f_pos'.
  255. *
  256. * If the end of the buffer has been reached, return -EAGAIN, if not,
  257. * return the offset within the buffer of the next entry to be
  258. * read.
  259. */
  260. static inline
  261. int find_dirent_index(nfs_readdir_descriptor_t *desc)
  262. {
  263. struct nfs_entry *entry = desc->entry;
  264. int loop_count = 0,
  265. status;
  266. for(;;) {
  267. status = dir_decode(desc);
  268. if (status)
  269. break;
  270. dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
  271. (unsigned long long)entry->cookie, desc->current_index);
  272. if (desc->file->f_pos == desc->current_index) {
  273. *desc->dir_cookie = entry->cookie;
  274. break;
  275. }
  276. desc->current_index++;
  277. if (loop_count++ > 200) {
  278. loop_count = 0;
  279. schedule();
  280. }
  281. }
  282. return status;
  283. }
  284. /*
  285. * Find the given page, and call find_dirent() or find_dirent_index in
  286. * order to try to return the next entry.
  287. */
  288. static inline
  289. int find_dirent_page(nfs_readdir_descriptor_t *desc)
  290. {
  291. struct inode *inode = desc->file->f_path.dentry->d_inode;
  292. struct page *page;
  293. int status;
  294. dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
  295. __func__, desc->page_index,
  296. (long long) *desc->dir_cookie);
  297. /* If we find the page in the page_cache, we cannot be sure
  298. * how fresh the data is, so we will ignore readdir_plus attributes.
  299. */
  300. desc->timestamp_valid = 0;
  301. page = read_cache_page(inode->i_mapping, desc->page_index,
  302. (filler_t *)nfs_readdir_filler, desc);
  303. if (IS_ERR(page)) {
  304. status = PTR_ERR(page);
  305. goto out;
  306. }
  307. /* NOTE: Someone else may have changed the READDIRPLUS flag */
  308. desc->page = page;
  309. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  310. if (*desc->dir_cookie != 0)
  311. status = find_dirent(desc);
  312. else
  313. status = find_dirent_index(desc);
  314. if (status < 0)
  315. dir_page_release(desc);
  316. out:
  317. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
  318. return status;
  319. }
  320. /*
  321. * Recurse through the page cache pages, and return a
  322. * filled nfs_entry structure of the next directory entry if possible.
  323. *
  324. * The target for the search is '*desc->dir_cookie' if non-0,
  325. * 'desc->file->f_pos' otherwise
  326. */
  327. static inline
  328. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  329. {
  330. int loop_count = 0;
  331. int res;
  332. /* Always search-by-index from the beginning of the cache */
  333. if (*desc->dir_cookie == 0) {
  334. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
  335. (long long)desc->file->f_pos);
  336. desc->page_index = 0;
  337. desc->entry->cookie = desc->entry->prev_cookie = 0;
  338. desc->entry->eof = 0;
  339. desc->current_index = 0;
  340. } else
  341. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
  342. (unsigned long long)*desc->dir_cookie);
  343. for (;;) {
  344. res = find_dirent_page(desc);
  345. if (res != -EAGAIN)
  346. break;
  347. /* Align to beginning of next page */
  348. desc->page_index ++;
  349. if (loop_count++ > 200) {
  350. loop_count = 0;
  351. schedule();
  352. }
  353. }
  354. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, res);
  355. return res;
  356. }
  357. static inline unsigned int dt_type(struct inode *inode)
  358. {
  359. return (inode->i_mode >> 12) & 15;
  360. }
  361. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
  362. /*
  363. * Once we've found the start of the dirent within a page: fill 'er up...
  364. */
  365. static
  366. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  367. filldir_t filldir)
  368. {
  369. struct file *file = desc->file;
  370. struct nfs_entry *entry = desc->entry;
  371. struct dentry *dentry = NULL;
  372. u64 fileid;
  373. int loop_count = 0,
  374. res;
  375. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
  376. (unsigned long long)entry->cookie);
  377. for(;;) {
  378. unsigned d_type = DT_UNKNOWN;
  379. /* Note: entry->prev_cookie contains the cookie for
  380. * retrieving the current dirent on the server */
  381. fileid = entry->ino;
  382. /* Get a dentry if we have one */
  383. if (dentry != NULL)
  384. dput(dentry);
  385. dentry = nfs_readdir_lookup(desc);
  386. /* Use readdirplus info */
  387. if (dentry != NULL && dentry->d_inode != NULL) {
  388. d_type = dt_type(dentry->d_inode);
  389. fileid = NFS_FILEID(dentry->d_inode);
  390. }
  391. res = filldir(dirent, entry->name, entry->len,
  392. file->f_pos, nfs_compat_user_ino64(fileid),
  393. d_type);
  394. if (res < 0)
  395. break;
  396. file->f_pos++;
  397. *desc->dir_cookie = entry->cookie;
  398. if (dir_decode(desc) != 0) {
  399. desc->page_index ++;
  400. break;
  401. }
  402. if (loop_count++ > 200) {
  403. loop_count = 0;
  404. schedule();
  405. }
  406. }
  407. dir_page_release(desc);
  408. if (dentry != NULL)
  409. dput(dentry);
  410. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  411. (unsigned long long)*desc->dir_cookie, res);
  412. return res;
  413. }
  414. /*
  415. * If we cannot find a cookie in our cache, we suspect that this is
  416. * because it points to a deleted file, so we ask the server to return
  417. * whatever it thinks is the next entry. We then feed this to filldir.
  418. * If all goes well, we should then be able to find our way round the
  419. * cache on the next call to readdir_search_pagecache();
  420. *
  421. * NOTE: we cannot add the anonymous page to the pagecache because
  422. * the data it contains might not be page aligned. Besides,
  423. * we should already have a complete representation of the
  424. * directory in the page cache by the time we get here.
  425. */
  426. static inline
  427. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  428. filldir_t filldir)
  429. {
  430. struct file *file = desc->file;
  431. struct inode *inode = file->f_path.dentry->d_inode;
  432. struct rpc_cred *cred = nfs_file_cred(file);
  433. struct page *page = NULL;
  434. int status;
  435. unsigned long timestamp, gencount;
  436. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  437. (unsigned long long)*desc->dir_cookie);
  438. page = alloc_page(GFP_HIGHUSER);
  439. if (!page) {
  440. status = -ENOMEM;
  441. goto out;
  442. }
  443. timestamp = jiffies;
  444. gencount = nfs_inc_attr_generation_counter();
  445. status = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred,
  446. *desc->dir_cookie, page,
  447. NFS_SERVER(inode)->dtsize,
  448. desc->plus);
  449. desc->page = page;
  450. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  451. if (status >= 0) {
  452. desc->timestamp = timestamp;
  453. desc->gencount = gencount;
  454. desc->timestamp_valid = 1;
  455. if ((status = dir_decode(desc)) == 0)
  456. desc->entry->prev_cookie = *desc->dir_cookie;
  457. } else
  458. status = -EIO;
  459. if (status < 0)
  460. goto out_release;
  461. status = nfs_do_filldir(desc, dirent, filldir);
  462. /* Reset read descriptor so it searches the page cache from
  463. * the start upon the next call to readdir_search_pagecache() */
  464. desc->page_index = 0;
  465. desc->entry->cookie = desc->entry->prev_cookie = 0;
  466. desc->entry->eof = 0;
  467. out:
  468. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  469. __func__, status);
  470. return status;
  471. out_release:
  472. dir_page_release(desc);
  473. goto out;
  474. }
  475. /* The file offset position represents the dirent entry number. A
  476. last cookie cache takes care of the common case of reading the
  477. whole directory.
  478. */
  479. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  480. {
  481. struct dentry *dentry = filp->f_path.dentry;
  482. struct inode *inode = dentry->d_inode;
  483. nfs_readdir_descriptor_t my_desc,
  484. *desc = &my_desc;
  485. struct nfs_entry my_entry;
  486. struct nfs_fh fh;
  487. struct nfs_fattr fattr;
  488. long res;
  489. dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
  490. dentry->d_parent->d_name.name, dentry->d_name.name,
  491. (long long)filp->f_pos);
  492. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  493. /*
  494. * filp->f_pos points to the dirent entry number.
  495. * *desc->dir_cookie has the cookie for the next entry. We have
  496. * to either find the entry with the appropriate number or
  497. * revalidate the cookie.
  498. */
  499. memset(desc, 0, sizeof(*desc));
  500. desc->file = filp;
  501. desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
  502. desc->decode = NFS_PROTO(inode)->decode_dirent;
  503. desc->plus = NFS_USE_READDIRPLUS(inode);
  504. my_entry.cookie = my_entry.prev_cookie = 0;
  505. my_entry.eof = 0;
  506. my_entry.fh = &fh;
  507. my_entry.fattr = &fattr;
  508. nfs_fattr_init(&fattr);
  509. desc->entry = &my_entry;
  510. nfs_block_sillyrename(dentry);
  511. res = nfs_revalidate_mapping_nolock(inode, filp->f_mapping);
  512. if (res < 0)
  513. goto out;
  514. while(!desc->entry->eof) {
  515. res = readdir_search_pagecache(desc);
  516. if (res == -EBADCOOKIE) {
  517. /* This means either end of directory */
  518. if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
  519. /* Or that the server has 'lost' a cookie */
  520. res = uncached_readdir(desc, dirent, filldir);
  521. if (res >= 0)
  522. continue;
  523. }
  524. res = 0;
  525. break;
  526. }
  527. if (res == -ETOOSMALL && desc->plus) {
  528. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  529. nfs_zap_caches(inode);
  530. desc->plus = 0;
  531. desc->entry->eof = 0;
  532. continue;
  533. }
  534. if (res < 0)
  535. break;
  536. res = nfs_do_filldir(desc, dirent, filldir);
  537. if (res < 0) {
  538. res = 0;
  539. break;
  540. }
  541. }
  542. out:
  543. nfs_unblock_sillyrename(dentry);
  544. if (res > 0)
  545. res = 0;
  546. dfprintk(FILE, "NFS: readdir(%s/%s) returns %ld\n",
  547. dentry->d_parent->d_name.name, dentry->d_name.name,
  548. res);
  549. return res;
  550. }
  551. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
  552. {
  553. struct dentry *dentry = filp->f_path.dentry;
  554. struct inode *inode = dentry->d_inode;
  555. dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
  556. dentry->d_parent->d_name.name,
  557. dentry->d_name.name,
  558. offset, origin);
  559. mutex_lock(&inode->i_mutex);
  560. switch (origin) {
  561. case 1:
  562. offset += filp->f_pos;
  563. case 0:
  564. if (offset >= 0)
  565. break;
  566. default:
  567. offset = -EINVAL;
  568. goto out;
  569. }
  570. if (offset != filp->f_pos) {
  571. filp->f_pos = offset;
  572. nfs_file_open_context(filp)->dir_cookie = 0;
  573. }
  574. out:
  575. mutex_unlock(&inode->i_mutex);
  576. return offset;
  577. }
  578. /*
  579. * All directory operations under NFS are synchronous, so fsync()
  580. * is a dummy operation.
  581. */
  582. static int nfs_fsync_dir(struct file *filp, struct dentry *dentry, int datasync)
  583. {
  584. dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
  585. dentry->d_parent->d_name.name, dentry->d_name.name,
  586. datasync);
  587. nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
  588. return 0;
  589. }
  590. /**
  591. * nfs_force_lookup_revalidate - Mark the directory as having changed
  592. * @dir - pointer to directory inode
  593. *
  594. * This forces the revalidation code in nfs_lookup_revalidate() to do a
  595. * full lookup on all child dentries of 'dir' whenever a change occurs
  596. * on the server that might have invalidated our dcache.
  597. *
  598. * The caller should be holding dir->i_lock
  599. */
  600. void nfs_force_lookup_revalidate(struct inode *dir)
  601. {
  602. NFS_I(dir)->cache_change_attribute++;
  603. }
  604. /*
  605. * A check for whether or not the parent directory has changed.
  606. * In the case it has, we assume that the dentries are untrustworthy
  607. * and may need to be looked up again.
  608. */
  609. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  610. {
  611. if (IS_ROOT(dentry))
  612. return 1;
  613. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  614. return 0;
  615. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  616. return 0;
  617. /* Revalidate nfsi->cache_change_attribute before we declare a match */
  618. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  619. return 0;
  620. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  621. return 0;
  622. return 1;
  623. }
  624. /*
  625. * Return the intent data that applies to this particular path component
  626. *
  627. * Note that the current set of intents only apply to the very last
  628. * component of the path.
  629. * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
  630. */
  631. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
  632. {
  633. if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
  634. return 0;
  635. return nd->flags & mask;
  636. }
  637. /*
  638. * Use intent information to check whether or not we're going to do
  639. * an O_EXCL create using this path component.
  640. */
  641. static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  642. {
  643. if (NFS_PROTO(dir)->version == 2)
  644. return 0;
  645. return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
  646. }
  647. /*
  648. * Inode and filehandle revalidation for lookups.
  649. *
  650. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  651. * or if the intent information indicates that we're about to open this
  652. * particular file and the "nocto" mount flag is not set.
  653. *
  654. */
  655. static inline
  656. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  657. {
  658. struct nfs_server *server = NFS_SERVER(inode);
  659. if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
  660. return 0;
  661. if (nd != NULL) {
  662. /* VFS wants an on-the-wire revalidation */
  663. if (nd->flags & LOOKUP_REVAL)
  664. goto out_force;
  665. /* This is an open(2) */
  666. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  667. !(server->flags & NFS_MOUNT_NOCTO) &&
  668. (S_ISREG(inode->i_mode) ||
  669. S_ISDIR(inode->i_mode)))
  670. goto out_force;
  671. return 0;
  672. }
  673. return nfs_revalidate_inode(server, inode);
  674. out_force:
  675. return __nfs_revalidate_inode(server, inode);
  676. }
  677. /*
  678. * We judge how long we want to trust negative
  679. * dentries by looking at the parent inode mtime.
  680. *
  681. * If parent mtime has changed, we revalidate, else we wait for a
  682. * period corresponding to the parent's attribute cache timeout value.
  683. */
  684. static inline
  685. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  686. struct nameidata *nd)
  687. {
  688. /* Don't revalidate a negative dentry if we're creating a new file */
  689. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  690. return 0;
  691. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  692. return 1;
  693. return !nfs_check_verifier(dir, dentry);
  694. }
  695. /*
  696. * This is called every time the dcache has a lookup hit,
  697. * and we should check whether we can really trust that
  698. * lookup.
  699. *
  700. * NOTE! The hit can be a negative hit too, don't assume
  701. * we have an inode!
  702. *
  703. * If the parent directory is seen to have changed, we throw out the
  704. * cached dentry and do a new lookup.
  705. */
  706. static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
  707. {
  708. struct inode *dir;
  709. struct inode *inode;
  710. struct dentry *parent;
  711. int error;
  712. struct nfs_fh fhandle;
  713. struct nfs_fattr fattr;
  714. parent = dget_parent(dentry);
  715. dir = parent->d_inode;
  716. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  717. inode = dentry->d_inode;
  718. if (!inode) {
  719. if (nfs_neg_need_reval(dir, dentry, nd))
  720. goto out_bad;
  721. goto out_valid;
  722. }
  723. if (is_bad_inode(inode)) {
  724. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  725. __func__, dentry->d_parent->d_name.name,
  726. dentry->d_name.name);
  727. goto out_bad;
  728. }
  729. /* Force a full look up iff the parent directory has changed */
  730. if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
  731. if (nfs_lookup_verify_inode(inode, nd))
  732. goto out_zap_parent;
  733. goto out_valid;
  734. }
  735. if (NFS_STALE(inode))
  736. goto out_bad;
  737. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  738. if (error)
  739. goto out_bad;
  740. if (nfs_compare_fh(NFS_FH(inode), &fhandle))
  741. goto out_bad;
  742. if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
  743. goto out_bad;
  744. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  745. out_valid:
  746. dput(parent);
  747. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  748. __func__, dentry->d_parent->d_name.name,
  749. dentry->d_name.name);
  750. return 1;
  751. out_zap_parent:
  752. nfs_zap_caches(dir);
  753. out_bad:
  754. nfs_mark_for_revalidate(dir);
  755. if (inode && S_ISDIR(inode->i_mode)) {
  756. /* Purge readdir caches. */
  757. nfs_zap_caches(inode);
  758. /* If we have submounts, don't unhash ! */
  759. if (have_submounts(dentry))
  760. goto out_valid;
  761. shrink_dcache_parent(dentry);
  762. }
  763. d_drop(dentry);
  764. dput(parent);
  765. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  766. __func__, dentry->d_parent->d_name.name,
  767. dentry->d_name.name);
  768. return 0;
  769. }
  770. /*
  771. * This is called from dput() when d_count is going to 0.
  772. */
  773. static int nfs_dentry_delete(struct dentry *dentry)
  774. {
  775. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  776. dentry->d_parent->d_name.name, dentry->d_name.name,
  777. dentry->d_flags);
  778. /* Unhash any dentry with a stale inode */
  779. if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
  780. return 1;
  781. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  782. /* Unhash it, so that ->d_iput() would be called */
  783. return 1;
  784. }
  785. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  786. /* Unhash it, so that ancestors of killed async unlink
  787. * files will be cleaned up during umount */
  788. return 1;
  789. }
  790. return 0;
  791. }
  792. static void nfs_drop_nlink(struct inode *inode)
  793. {
  794. spin_lock(&inode->i_lock);
  795. if (inode->i_nlink > 0)
  796. drop_nlink(inode);
  797. spin_unlock(&inode->i_lock);
  798. }
  799. /*
  800. * Called when the dentry loses inode.
  801. * We use it to clean up silly-renamed files.
  802. */
  803. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  804. {
  805. if (S_ISDIR(inode->i_mode))
  806. /* drop any readdir cache as it could easily be old */
  807. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  808. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  809. drop_nlink(inode);
  810. nfs_complete_unlink(dentry, inode);
  811. }
  812. iput(inode);
  813. }
  814. struct dentry_operations nfs_dentry_operations = {
  815. .d_revalidate = nfs_lookup_revalidate,
  816. .d_delete = nfs_dentry_delete,
  817. .d_iput = nfs_dentry_iput,
  818. };
  819. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  820. {
  821. struct dentry *res;
  822. struct dentry *parent;
  823. struct inode *inode = NULL;
  824. int error;
  825. struct nfs_fh fhandle;
  826. struct nfs_fattr fattr;
  827. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  828. dentry->d_parent->d_name.name, dentry->d_name.name);
  829. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  830. res = ERR_PTR(-ENAMETOOLONG);
  831. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  832. goto out;
  833. res = ERR_PTR(-ENOMEM);
  834. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  835. /*
  836. * If we're doing an exclusive create, optimize away the lookup
  837. * but don't hash the dentry.
  838. */
  839. if (nfs_is_exclusive_create(dir, nd)) {
  840. d_instantiate(dentry, NULL);
  841. res = NULL;
  842. goto out;
  843. }
  844. parent = dentry->d_parent;
  845. /* Protect against concurrent sillydeletes */
  846. nfs_block_sillyrename(parent);
  847. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  848. if (error == -ENOENT)
  849. goto no_entry;
  850. if (error < 0) {
  851. res = ERR_PTR(error);
  852. goto out_unblock_sillyrename;
  853. }
  854. inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
  855. res = (struct dentry *)inode;
  856. if (IS_ERR(res))
  857. goto out_unblock_sillyrename;
  858. no_entry:
  859. res = d_materialise_unique(dentry, inode);
  860. if (res != NULL) {
  861. if (IS_ERR(res))
  862. goto out_unblock_sillyrename;
  863. dentry = res;
  864. }
  865. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  866. out_unblock_sillyrename:
  867. nfs_unblock_sillyrename(parent);
  868. out:
  869. return res;
  870. }
  871. #ifdef CONFIG_NFS_V4
  872. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  873. struct dentry_operations nfs4_dentry_operations = {
  874. .d_revalidate = nfs_open_revalidate,
  875. .d_delete = nfs_dentry_delete,
  876. .d_iput = nfs_dentry_iput,
  877. };
  878. /*
  879. * Use intent information to determine whether we need to substitute
  880. * the NFSv4-style stateful OPEN for the LOOKUP call
  881. */
  882. static int is_atomic_open(struct nameidata *nd)
  883. {
  884. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  885. return 0;
  886. /* NFS does not (yet) have a stateful open for directories */
  887. if (nd->flags & LOOKUP_DIRECTORY)
  888. return 0;
  889. /* Are we trying to write to a read only partition? */
  890. if (__mnt_is_readonly(nd->path.mnt) &&
  891. (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  892. return 0;
  893. return 1;
  894. }
  895. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  896. {
  897. struct dentry *res = NULL;
  898. int error;
  899. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  900. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  901. /* Check that we are indeed trying to open this file */
  902. if (!is_atomic_open(nd))
  903. goto no_open;
  904. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  905. res = ERR_PTR(-ENAMETOOLONG);
  906. goto out;
  907. }
  908. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  909. /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
  910. * the dentry. */
  911. if (nd->flags & LOOKUP_EXCL) {
  912. d_instantiate(dentry, NULL);
  913. goto out;
  914. }
  915. /* Open the file on the server */
  916. res = nfs4_atomic_open(dir, dentry, nd);
  917. if (IS_ERR(res)) {
  918. error = PTR_ERR(res);
  919. switch (error) {
  920. /* Make a negative dentry */
  921. case -ENOENT:
  922. res = NULL;
  923. goto out;
  924. /* This turned out not to be a regular file */
  925. case -EISDIR:
  926. case -ENOTDIR:
  927. goto no_open;
  928. case -ELOOP:
  929. if (!(nd->intent.open.flags & O_NOFOLLOW))
  930. goto no_open;
  931. /* case -EINVAL: */
  932. default:
  933. goto out;
  934. }
  935. } else if (res != NULL)
  936. dentry = res;
  937. out:
  938. return res;
  939. no_open:
  940. return nfs_lookup(dir, dentry, nd);
  941. }
  942. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  943. {
  944. struct dentry *parent = NULL;
  945. struct inode *inode = dentry->d_inode;
  946. struct inode *dir;
  947. int openflags, ret = 0;
  948. if (!is_atomic_open(nd))
  949. goto no_open;
  950. parent = dget_parent(dentry);
  951. dir = parent->d_inode;
  952. /* We can't create new files in nfs_open_revalidate(), so we
  953. * optimize away revalidation of negative dentries.
  954. */
  955. if (inode == NULL) {
  956. if (!nfs_neg_need_reval(dir, dentry, nd))
  957. ret = 1;
  958. goto out;
  959. }
  960. /* NFS only supports OPEN on regular files */
  961. if (!S_ISREG(inode->i_mode))
  962. goto no_open_dput;
  963. openflags = nd->intent.open.flags;
  964. /* We cannot do exclusive creation on a positive dentry */
  965. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  966. goto no_open_dput;
  967. /* We can't create new files, or truncate existing ones here */
  968. openflags &= ~(O_CREAT|O_TRUNC);
  969. /*
  970. * Note: we're not holding inode->i_mutex and so may be racing with
  971. * operations that change the directory. We therefore save the
  972. * change attribute *before* we do the RPC call.
  973. */
  974. ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
  975. out:
  976. dput(parent);
  977. if (!ret)
  978. d_drop(dentry);
  979. return ret;
  980. no_open_dput:
  981. dput(parent);
  982. no_open:
  983. if (inode != NULL && nfs_have_delegation(inode, FMODE_READ))
  984. return 1;
  985. return nfs_lookup_revalidate(dentry, nd);
  986. }
  987. #endif /* CONFIG_NFSV4 */
  988. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  989. {
  990. struct dentry *parent = desc->file->f_path.dentry;
  991. struct inode *dir = parent->d_inode;
  992. struct nfs_entry *entry = desc->entry;
  993. struct dentry *dentry, *alias;
  994. struct qstr name = {
  995. .name = entry->name,
  996. .len = entry->len,
  997. };
  998. struct inode *inode;
  999. unsigned long verf = nfs_save_change_attribute(dir);
  1000. switch (name.len) {
  1001. case 2:
  1002. if (name.name[0] == '.' && name.name[1] == '.')
  1003. return dget_parent(parent);
  1004. break;
  1005. case 1:
  1006. if (name.name[0] == '.')
  1007. return dget(parent);
  1008. }
  1009. spin_lock(&dir->i_lock);
  1010. if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) {
  1011. spin_unlock(&dir->i_lock);
  1012. return NULL;
  1013. }
  1014. spin_unlock(&dir->i_lock);
  1015. name.hash = full_name_hash(name.name, name.len);
  1016. dentry = d_lookup(parent, &name);
  1017. if (dentry != NULL) {
  1018. /* Is this a positive dentry that matches the readdir info? */
  1019. if (dentry->d_inode != NULL &&
  1020. (NFS_FILEID(dentry->d_inode) == entry->ino ||
  1021. d_mountpoint(dentry))) {
  1022. if (!desc->plus || entry->fh->size == 0)
  1023. return dentry;
  1024. if (nfs_compare_fh(NFS_FH(dentry->d_inode),
  1025. entry->fh) == 0)
  1026. goto out_renew;
  1027. }
  1028. /* No, so d_drop to allow one to be created */
  1029. d_drop(dentry);
  1030. dput(dentry);
  1031. }
  1032. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  1033. return NULL;
  1034. if (name.len > NFS_SERVER(dir)->namelen)
  1035. return NULL;
  1036. /* Note: caller is already holding the dir->i_mutex! */
  1037. dentry = d_alloc(parent, &name);
  1038. if (dentry == NULL)
  1039. return NULL;
  1040. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  1041. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  1042. if (IS_ERR(inode)) {
  1043. dput(dentry);
  1044. return NULL;
  1045. }
  1046. alias = d_materialise_unique(dentry, inode);
  1047. if (alias != NULL) {
  1048. dput(dentry);
  1049. if (IS_ERR(alias))
  1050. return NULL;
  1051. dentry = alias;
  1052. }
  1053. out_renew:
  1054. nfs_set_verifier(dentry, verf);
  1055. return dentry;
  1056. }
  1057. /*
  1058. * Code common to create, mkdir, and mknod.
  1059. */
  1060. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1061. struct nfs_fattr *fattr)
  1062. {
  1063. struct dentry *parent = dget_parent(dentry);
  1064. struct inode *dir = parent->d_inode;
  1065. struct inode *inode;
  1066. int error = -EACCES;
  1067. d_drop(dentry);
  1068. /* We may have been initialized further down */
  1069. if (dentry->d_inode)
  1070. goto out;
  1071. if (fhandle->size == 0) {
  1072. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1073. if (error)
  1074. goto out_error;
  1075. }
  1076. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1077. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1078. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1079. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1080. if (error < 0)
  1081. goto out_error;
  1082. }
  1083. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1084. error = PTR_ERR(inode);
  1085. if (IS_ERR(inode))
  1086. goto out_error;
  1087. d_add(dentry, inode);
  1088. out:
  1089. dput(parent);
  1090. return 0;
  1091. out_error:
  1092. nfs_mark_for_revalidate(dir);
  1093. dput(parent);
  1094. return error;
  1095. }
  1096. /*
  1097. * Following a failed create operation, we drop the dentry rather
  1098. * than retain a negative dentry. This avoids a problem in the event
  1099. * that the operation succeeded on the server, but an error in the
  1100. * reply path made it appear to have failed.
  1101. */
  1102. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1103. struct nameidata *nd)
  1104. {
  1105. struct iattr attr;
  1106. int error;
  1107. int open_flags = 0;
  1108. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1109. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1110. attr.ia_mode = mode;
  1111. attr.ia_valid = ATTR_MODE;
  1112. if ((nd->flags & LOOKUP_CREATE) != 0)
  1113. open_flags = nd->intent.open.flags;
  1114. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
  1115. if (error != 0)
  1116. goto out_err;
  1117. return 0;
  1118. out_err:
  1119. d_drop(dentry);
  1120. return error;
  1121. }
  1122. /*
  1123. * See comments for nfs_proc_create regarding failed operations.
  1124. */
  1125. static int
  1126. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1127. {
  1128. struct iattr attr;
  1129. int status;
  1130. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1131. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1132. if (!new_valid_dev(rdev))
  1133. return -EINVAL;
  1134. attr.ia_mode = mode;
  1135. attr.ia_valid = ATTR_MODE;
  1136. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1137. if (status != 0)
  1138. goto out_err;
  1139. return 0;
  1140. out_err:
  1141. d_drop(dentry);
  1142. return status;
  1143. }
  1144. /*
  1145. * See comments for nfs_proc_create regarding failed operations.
  1146. */
  1147. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1148. {
  1149. struct iattr attr;
  1150. int error;
  1151. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1152. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1153. attr.ia_valid = ATTR_MODE;
  1154. attr.ia_mode = mode | S_IFDIR;
  1155. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1156. if (error != 0)
  1157. goto out_err;
  1158. return 0;
  1159. out_err:
  1160. d_drop(dentry);
  1161. return error;
  1162. }
  1163. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1164. {
  1165. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1166. d_delete(dentry);
  1167. }
  1168. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1169. {
  1170. int error;
  1171. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1172. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1173. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1174. /* Ensure the VFS deletes this inode */
  1175. if (error == 0 && dentry->d_inode != NULL)
  1176. clear_nlink(dentry->d_inode);
  1177. else if (error == -ENOENT)
  1178. nfs_dentry_handle_enoent(dentry);
  1179. return error;
  1180. }
  1181. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1182. {
  1183. static unsigned int sillycounter;
  1184. const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
  1185. const int countersize = sizeof(sillycounter)*2;
  1186. const int slen = sizeof(".nfs")+fileidsize+countersize-1;
  1187. char silly[slen+1];
  1188. struct qstr qsilly;
  1189. struct dentry *sdentry;
  1190. int error = -EIO;
  1191. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1192. dentry->d_parent->d_name.name, dentry->d_name.name,
  1193. atomic_read(&dentry->d_count));
  1194. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  1195. /*
  1196. * We don't allow a dentry to be silly-renamed twice.
  1197. */
  1198. error = -EBUSY;
  1199. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1200. goto out;
  1201. sprintf(silly, ".nfs%*.*Lx",
  1202. fileidsize, fileidsize,
  1203. (unsigned long long)NFS_FILEID(dentry->d_inode));
  1204. /* Return delegation in anticipation of the rename */
  1205. nfs_inode_return_delegation(dentry->d_inode);
  1206. sdentry = NULL;
  1207. do {
  1208. char *suffix = silly + slen - countersize;
  1209. dput(sdentry);
  1210. sillycounter++;
  1211. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1212. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  1213. dentry->d_name.name, silly);
  1214. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1215. /*
  1216. * N.B. Better to return EBUSY here ... it could be
  1217. * dangerous to delete the file while it's in use.
  1218. */
  1219. if (IS_ERR(sdentry))
  1220. goto out;
  1221. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1222. qsilly.name = silly;
  1223. qsilly.len = strlen(silly);
  1224. if (dentry->d_inode) {
  1225. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1226. dir, &qsilly);
  1227. nfs_mark_for_revalidate(dentry->d_inode);
  1228. } else
  1229. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1230. dir, &qsilly);
  1231. if (!error) {
  1232. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1233. d_move(dentry, sdentry);
  1234. error = nfs_async_unlink(dir, dentry);
  1235. /* If we return 0 we don't unlink */
  1236. }
  1237. dput(sdentry);
  1238. out:
  1239. return error;
  1240. }
  1241. /*
  1242. * Remove a file after making sure there are no pending writes,
  1243. * and after checking that the file has only one user.
  1244. *
  1245. * We invalidate the attribute cache and free the inode prior to the operation
  1246. * to avoid possible races if the server reuses the inode.
  1247. */
  1248. static int nfs_safe_remove(struct dentry *dentry)
  1249. {
  1250. struct inode *dir = dentry->d_parent->d_inode;
  1251. struct inode *inode = dentry->d_inode;
  1252. int error = -EBUSY;
  1253. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1254. dentry->d_parent->d_name.name, dentry->d_name.name);
  1255. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1256. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1257. error = 0;
  1258. goto out;
  1259. }
  1260. if (inode != NULL) {
  1261. nfs_inode_return_delegation(inode);
  1262. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1263. /* The VFS may want to delete this inode */
  1264. if (error == 0)
  1265. nfs_drop_nlink(inode);
  1266. nfs_mark_for_revalidate(inode);
  1267. } else
  1268. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1269. if (error == -ENOENT)
  1270. nfs_dentry_handle_enoent(dentry);
  1271. out:
  1272. return error;
  1273. }
  1274. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1275. * belongs to an active ".nfs..." file and we return -EBUSY.
  1276. *
  1277. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1278. */
  1279. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1280. {
  1281. int error;
  1282. int need_rehash = 0;
  1283. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1284. dir->i_ino, dentry->d_name.name);
  1285. spin_lock(&dcache_lock);
  1286. spin_lock(&dentry->d_lock);
  1287. if (atomic_read(&dentry->d_count) > 1) {
  1288. spin_unlock(&dentry->d_lock);
  1289. spin_unlock(&dcache_lock);
  1290. /* Start asynchronous writeout of the inode */
  1291. write_inode_now(dentry->d_inode, 0);
  1292. error = nfs_sillyrename(dir, dentry);
  1293. return error;
  1294. }
  1295. if (!d_unhashed(dentry)) {
  1296. __d_drop(dentry);
  1297. need_rehash = 1;
  1298. }
  1299. spin_unlock(&dentry->d_lock);
  1300. spin_unlock(&dcache_lock);
  1301. error = nfs_safe_remove(dentry);
  1302. if (!error || error == -ENOENT) {
  1303. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1304. } else if (need_rehash)
  1305. d_rehash(dentry);
  1306. return error;
  1307. }
  1308. /*
  1309. * To create a symbolic link, most file systems instantiate a new inode,
  1310. * add a page to it containing the path, then write it out to the disk
  1311. * using prepare_write/commit_write.
  1312. *
  1313. * Unfortunately the NFS client can't create the in-core inode first
  1314. * because it needs a file handle to create an in-core inode (see
  1315. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1316. * symlink request has completed on the server.
  1317. *
  1318. * So instead we allocate a raw page, copy the symname into it, then do
  1319. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1320. * now have a new file handle and can instantiate an in-core NFS inode
  1321. * and move the raw page into its mapping.
  1322. */
  1323. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1324. {
  1325. struct pagevec lru_pvec;
  1326. struct page *page;
  1327. char *kaddr;
  1328. struct iattr attr;
  1329. unsigned int pathlen = strlen(symname);
  1330. int error;
  1331. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1332. dir->i_ino, dentry->d_name.name, symname);
  1333. if (pathlen > PAGE_SIZE)
  1334. return -ENAMETOOLONG;
  1335. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1336. attr.ia_valid = ATTR_MODE;
  1337. page = alloc_page(GFP_HIGHUSER);
  1338. if (!page)
  1339. return -ENOMEM;
  1340. kaddr = kmap_atomic(page, KM_USER0);
  1341. memcpy(kaddr, symname, pathlen);
  1342. if (pathlen < PAGE_SIZE)
  1343. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1344. kunmap_atomic(kaddr, KM_USER0);
  1345. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1346. if (error != 0) {
  1347. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1348. dir->i_sb->s_id, dir->i_ino,
  1349. dentry->d_name.name, symname, error);
  1350. d_drop(dentry);
  1351. __free_page(page);
  1352. return error;
  1353. }
  1354. /*
  1355. * No big deal if we can't add this page to the page cache here.
  1356. * READLINK will get the missing page from the server if needed.
  1357. */
  1358. pagevec_init(&lru_pvec, 0);
  1359. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1360. GFP_KERNEL)) {
  1361. pagevec_add(&lru_pvec, page);
  1362. pagevec_lru_add_file(&lru_pvec);
  1363. SetPageUptodate(page);
  1364. unlock_page(page);
  1365. } else
  1366. __free_page(page);
  1367. return 0;
  1368. }
  1369. static int
  1370. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1371. {
  1372. struct inode *inode = old_dentry->d_inode;
  1373. int error;
  1374. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1375. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1376. dentry->d_parent->d_name.name, dentry->d_name.name);
  1377. d_drop(dentry);
  1378. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1379. if (error == 0) {
  1380. atomic_inc(&inode->i_count);
  1381. d_add(dentry, inode);
  1382. }
  1383. return error;
  1384. }
  1385. /*
  1386. * RENAME
  1387. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1388. * different file handle for the same inode after a rename (e.g. when
  1389. * moving to a different directory). A fail-safe method to do so would
  1390. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1391. * rename the old file using the sillyrename stuff. This way, the original
  1392. * file in old_dir will go away when the last process iput()s the inode.
  1393. *
  1394. * FIXED.
  1395. *
  1396. * It actually works quite well. One needs to have the possibility for
  1397. * at least one ".nfs..." file in each directory the file ever gets
  1398. * moved or linked to which happens automagically with the new
  1399. * implementation that only depends on the dcache stuff instead of
  1400. * using the inode layer
  1401. *
  1402. * Unfortunately, things are a little more complicated than indicated
  1403. * above. For a cross-directory move, we want to make sure we can get
  1404. * rid of the old inode after the operation. This means there must be
  1405. * no pending writes (if it's a file), and the use count must be 1.
  1406. * If these conditions are met, we can drop the dentries before doing
  1407. * the rename.
  1408. */
  1409. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1410. struct inode *new_dir, struct dentry *new_dentry)
  1411. {
  1412. struct inode *old_inode = old_dentry->d_inode;
  1413. struct inode *new_inode = new_dentry->d_inode;
  1414. struct dentry *dentry = NULL, *rehash = NULL;
  1415. int error = -EBUSY;
  1416. /*
  1417. * To prevent any new references to the target during the rename,
  1418. * we unhash the dentry and free the inode in advance.
  1419. */
  1420. if (!d_unhashed(new_dentry)) {
  1421. d_drop(new_dentry);
  1422. rehash = new_dentry;
  1423. }
  1424. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1425. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1426. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1427. atomic_read(&new_dentry->d_count));
  1428. /*
  1429. * First check whether the target is busy ... we can't
  1430. * safely do _any_ rename if the target is in use.
  1431. *
  1432. * For files, make a copy of the dentry and then do a
  1433. * silly-rename. If the silly-rename succeeds, the
  1434. * copied dentry is hashed and becomes the new target.
  1435. */
  1436. if (!new_inode)
  1437. goto go_ahead;
  1438. if (S_ISDIR(new_inode->i_mode)) {
  1439. error = -EISDIR;
  1440. if (!S_ISDIR(old_inode->i_mode))
  1441. goto out;
  1442. } else if (atomic_read(&new_dentry->d_count) > 2) {
  1443. int err;
  1444. /* copy the target dentry's name */
  1445. dentry = d_alloc(new_dentry->d_parent,
  1446. &new_dentry->d_name);
  1447. if (!dentry)
  1448. goto out;
  1449. /* silly-rename the existing target ... */
  1450. err = nfs_sillyrename(new_dir, new_dentry);
  1451. if (!err) {
  1452. new_dentry = rehash = dentry;
  1453. new_inode = NULL;
  1454. /* instantiate the replacement target */
  1455. d_instantiate(new_dentry, NULL);
  1456. } else if (atomic_read(&new_dentry->d_count) > 1)
  1457. /* dentry still busy? */
  1458. goto out;
  1459. } else
  1460. nfs_drop_nlink(new_inode);
  1461. go_ahead:
  1462. /*
  1463. * ... prune child dentries and writebacks if needed.
  1464. */
  1465. if (atomic_read(&old_dentry->d_count) > 1) {
  1466. if (S_ISREG(old_inode->i_mode))
  1467. nfs_wb_all(old_inode);
  1468. shrink_dcache_parent(old_dentry);
  1469. }
  1470. nfs_inode_return_delegation(old_inode);
  1471. if (new_inode != NULL) {
  1472. nfs_inode_return_delegation(new_inode);
  1473. d_delete(new_dentry);
  1474. }
  1475. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1476. new_dir, &new_dentry->d_name);
  1477. nfs_mark_for_revalidate(old_inode);
  1478. out:
  1479. if (rehash)
  1480. d_rehash(rehash);
  1481. if (!error) {
  1482. d_move(old_dentry, new_dentry);
  1483. nfs_set_verifier(new_dentry,
  1484. nfs_save_change_attribute(new_dir));
  1485. } else if (error == -ENOENT)
  1486. nfs_dentry_handle_enoent(old_dentry);
  1487. /* new dentry created? */
  1488. if (dentry)
  1489. dput(dentry);
  1490. return error;
  1491. }
  1492. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1493. static LIST_HEAD(nfs_access_lru_list);
  1494. static atomic_long_t nfs_access_nr_entries;
  1495. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1496. {
  1497. put_rpccred(entry->cred);
  1498. kfree(entry);
  1499. smp_mb__before_atomic_dec();
  1500. atomic_long_dec(&nfs_access_nr_entries);
  1501. smp_mb__after_atomic_dec();
  1502. }
  1503. int nfs_access_cache_shrinker(int nr_to_scan, gfp_t gfp_mask)
  1504. {
  1505. LIST_HEAD(head);
  1506. struct nfs_inode *nfsi;
  1507. struct nfs_access_entry *cache;
  1508. restart:
  1509. spin_lock(&nfs_access_lru_lock);
  1510. list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
  1511. struct rw_semaphore *s_umount;
  1512. struct inode *inode;
  1513. if (nr_to_scan-- == 0)
  1514. break;
  1515. s_umount = &nfsi->vfs_inode.i_sb->s_umount;
  1516. if (!down_read_trylock(s_umount))
  1517. continue;
  1518. inode = igrab(&nfsi->vfs_inode);
  1519. if (inode == NULL) {
  1520. up_read(s_umount);
  1521. continue;
  1522. }
  1523. spin_lock(&inode->i_lock);
  1524. if (list_empty(&nfsi->access_cache_entry_lru))
  1525. goto remove_lru_entry;
  1526. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1527. struct nfs_access_entry, lru);
  1528. list_move(&cache->lru, &head);
  1529. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1530. if (!list_empty(&nfsi->access_cache_entry_lru))
  1531. list_move_tail(&nfsi->access_cache_inode_lru,
  1532. &nfs_access_lru_list);
  1533. else {
  1534. remove_lru_entry:
  1535. list_del_init(&nfsi->access_cache_inode_lru);
  1536. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1537. }
  1538. spin_unlock(&inode->i_lock);
  1539. spin_unlock(&nfs_access_lru_lock);
  1540. iput(inode);
  1541. up_read(s_umount);
  1542. goto restart;
  1543. }
  1544. spin_unlock(&nfs_access_lru_lock);
  1545. while (!list_empty(&head)) {
  1546. cache = list_entry(head.next, struct nfs_access_entry, lru);
  1547. list_del(&cache->lru);
  1548. nfs_access_free_entry(cache);
  1549. }
  1550. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1551. }
  1552. static void __nfs_access_zap_cache(struct inode *inode)
  1553. {
  1554. struct nfs_inode *nfsi = NFS_I(inode);
  1555. struct rb_root *root_node = &nfsi->access_cache;
  1556. struct rb_node *n, *dispose = NULL;
  1557. struct nfs_access_entry *entry;
  1558. /* Unhook entries from the cache */
  1559. while ((n = rb_first(root_node)) != NULL) {
  1560. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1561. rb_erase(n, root_node);
  1562. list_del(&entry->lru);
  1563. n->rb_left = dispose;
  1564. dispose = n;
  1565. }
  1566. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1567. spin_unlock(&inode->i_lock);
  1568. /* Now kill them all! */
  1569. while (dispose != NULL) {
  1570. n = dispose;
  1571. dispose = n->rb_left;
  1572. nfs_access_free_entry(rb_entry(n, struct nfs_access_entry, rb_node));
  1573. }
  1574. }
  1575. void nfs_access_zap_cache(struct inode *inode)
  1576. {
  1577. /* Remove from global LRU init */
  1578. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1579. spin_lock(&nfs_access_lru_lock);
  1580. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1581. spin_unlock(&nfs_access_lru_lock);
  1582. }
  1583. spin_lock(&inode->i_lock);
  1584. /* This will release the spinlock */
  1585. __nfs_access_zap_cache(inode);
  1586. }
  1587. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1588. {
  1589. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1590. struct nfs_access_entry *entry;
  1591. while (n != NULL) {
  1592. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1593. if (cred < entry->cred)
  1594. n = n->rb_left;
  1595. else if (cred > entry->cred)
  1596. n = n->rb_right;
  1597. else
  1598. return entry;
  1599. }
  1600. return NULL;
  1601. }
  1602. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1603. {
  1604. struct nfs_inode *nfsi = NFS_I(inode);
  1605. struct nfs_access_entry *cache;
  1606. int err = -ENOENT;
  1607. spin_lock(&inode->i_lock);
  1608. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1609. goto out_zap;
  1610. cache = nfs_access_search_rbtree(inode, cred);
  1611. if (cache == NULL)
  1612. goto out;
  1613. if (!time_in_range(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1614. goto out_stale;
  1615. res->jiffies = cache->jiffies;
  1616. res->cred = cache->cred;
  1617. res->mask = cache->mask;
  1618. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1619. err = 0;
  1620. out:
  1621. spin_unlock(&inode->i_lock);
  1622. return err;
  1623. out_stale:
  1624. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1625. list_del(&cache->lru);
  1626. spin_unlock(&inode->i_lock);
  1627. nfs_access_free_entry(cache);
  1628. return -ENOENT;
  1629. out_zap:
  1630. /* This will release the spinlock */
  1631. __nfs_access_zap_cache(inode);
  1632. return -ENOENT;
  1633. }
  1634. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1635. {
  1636. struct nfs_inode *nfsi = NFS_I(inode);
  1637. struct rb_root *root_node = &nfsi->access_cache;
  1638. struct rb_node **p = &root_node->rb_node;
  1639. struct rb_node *parent = NULL;
  1640. struct nfs_access_entry *entry;
  1641. spin_lock(&inode->i_lock);
  1642. while (*p != NULL) {
  1643. parent = *p;
  1644. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1645. if (set->cred < entry->cred)
  1646. p = &parent->rb_left;
  1647. else if (set->cred > entry->cred)
  1648. p = &parent->rb_right;
  1649. else
  1650. goto found;
  1651. }
  1652. rb_link_node(&set->rb_node, parent, p);
  1653. rb_insert_color(&set->rb_node, root_node);
  1654. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1655. spin_unlock(&inode->i_lock);
  1656. return;
  1657. found:
  1658. rb_replace_node(parent, &set->rb_node, root_node);
  1659. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1660. list_del(&entry->lru);
  1661. spin_unlock(&inode->i_lock);
  1662. nfs_access_free_entry(entry);
  1663. }
  1664. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1665. {
  1666. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1667. if (cache == NULL)
  1668. return;
  1669. RB_CLEAR_NODE(&cache->rb_node);
  1670. cache->jiffies = set->jiffies;
  1671. cache->cred = get_rpccred(set->cred);
  1672. cache->mask = set->mask;
  1673. nfs_access_add_rbtree(inode, cache);
  1674. /* Update accounting */
  1675. smp_mb__before_atomic_inc();
  1676. atomic_long_inc(&nfs_access_nr_entries);
  1677. smp_mb__after_atomic_inc();
  1678. /* Add inode to global LRU list */
  1679. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1680. spin_lock(&nfs_access_lru_lock);
  1681. list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
  1682. spin_unlock(&nfs_access_lru_lock);
  1683. }
  1684. }
  1685. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1686. {
  1687. struct nfs_access_entry cache;
  1688. int status;
  1689. status = nfs_access_get_cached(inode, cred, &cache);
  1690. if (status == 0)
  1691. goto out;
  1692. /* Be clever: ask server to check for all possible rights */
  1693. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1694. cache.cred = cred;
  1695. cache.jiffies = jiffies;
  1696. status = NFS_PROTO(inode)->access(inode, &cache);
  1697. if (status != 0)
  1698. return status;
  1699. nfs_access_add_cache(inode, &cache);
  1700. out:
  1701. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1702. return 0;
  1703. return -EACCES;
  1704. }
  1705. static int nfs_open_permission_mask(int openflags)
  1706. {
  1707. int mask = 0;
  1708. if (openflags & FMODE_READ)
  1709. mask |= MAY_READ;
  1710. if (openflags & FMODE_WRITE)
  1711. mask |= MAY_WRITE;
  1712. if (openflags & FMODE_EXEC)
  1713. mask |= MAY_EXEC;
  1714. return mask;
  1715. }
  1716. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  1717. {
  1718. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  1719. }
  1720. int nfs_permission(struct inode *inode, int mask)
  1721. {
  1722. struct rpc_cred *cred;
  1723. int res = 0;
  1724. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1725. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1726. goto out;
  1727. /* Is this sys_access() ? */
  1728. if (mask & MAY_ACCESS)
  1729. goto force_lookup;
  1730. switch (inode->i_mode & S_IFMT) {
  1731. case S_IFLNK:
  1732. goto out;
  1733. case S_IFREG:
  1734. /* NFSv4 has atomic_open... */
  1735. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1736. && (mask & MAY_OPEN))
  1737. goto out;
  1738. break;
  1739. case S_IFDIR:
  1740. /*
  1741. * Optimize away all write operations, since the server
  1742. * will check permissions when we perform the op.
  1743. */
  1744. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1745. goto out;
  1746. }
  1747. force_lookup:
  1748. if (!NFS_PROTO(inode)->access)
  1749. goto out_notsup;
  1750. cred = rpc_lookup_cred();
  1751. if (!IS_ERR(cred)) {
  1752. res = nfs_do_access(inode, cred, mask);
  1753. put_rpccred(cred);
  1754. } else
  1755. res = PTR_ERR(cred);
  1756. out:
  1757. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  1758. res = -EACCES;
  1759. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  1760. inode->i_sb->s_id, inode->i_ino, mask, res);
  1761. return res;
  1762. out_notsup:
  1763. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1764. if (res == 0)
  1765. res = generic_permission(inode, mask, NULL);
  1766. goto out;
  1767. }
  1768. /*
  1769. * Local variables:
  1770. * version-control: t
  1771. * kept-new-versions: 5
  1772. * End:
  1773. */