dir.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  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. if (nfs_have_delegation(inode, FMODE_READ))
  730. goto out_set_verifier;
  731. /* Force a full look up iff the parent directory has changed */
  732. if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
  733. if (nfs_lookup_verify_inode(inode, nd))
  734. goto out_zap_parent;
  735. goto out_valid;
  736. }
  737. if (NFS_STALE(inode))
  738. goto out_bad;
  739. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  740. if (error)
  741. goto out_bad;
  742. if (nfs_compare_fh(NFS_FH(inode), &fhandle))
  743. goto out_bad;
  744. if ((error = nfs_refresh_inode(inode, &fattr)) != 0)
  745. goto out_bad;
  746. out_set_verifier:
  747. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  748. out_valid:
  749. dput(parent);
  750. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  751. __func__, dentry->d_parent->d_name.name,
  752. dentry->d_name.name);
  753. return 1;
  754. out_zap_parent:
  755. nfs_zap_caches(dir);
  756. out_bad:
  757. nfs_mark_for_revalidate(dir);
  758. if (inode && S_ISDIR(inode->i_mode)) {
  759. /* Purge readdir caches. */
  760. nfs_zap_caches(inode);
  761. /* If we have submounts, don't unhash ! */
  762. if (have_submounts(dentry))
  763. goto out_valid;
  764. shrink_dcache_parent(dentry);
  765. }
  766. d_drop(dentry);
  767. dput(parent);
  768. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  769. __func__, dentry->d_parent->d_name.name,
  770. dentry->d_name.name);
  771. return 0;
  772. }
  773. /*
  774. * This is called from dput() when d_count is going to 0.
  775. */
  776. static int nfs_dentry_delete(struct dentry *dentry)
  777. {
  778. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  779. dentry->d_parent->d_name.name, dentry->d_name.name,
  780. dentry->d_flags);
  781. /* Unhash any dentry with a stale inode */
  782. if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
  783. return 1;
  784. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  785. /* Unhash it, so that ->d_iput() would be called */
  786. return 1;
  787. }
  788. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  789. /* Unhash it, so that ancestors of killed async unlink
  790. * files will be cleaned up during umount */
  791. return 1;
  792. }
  793. return 0;
  794. }
  795. static void nfs_drop_nlink(struct inode *inode)
  796. {
  797. spin_lock(&inode->i_lock);
  798. if (inode->i_nlink > 0)
  799. drop_nlink(inode);
  800. spin_unlock(&inode->i_lock);
  801. }
  802. /*
  803. * Called when the dentry loses inode.
  804. * We use it to clean up silly-renamed files.
  805. */
  806. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  807. {
  808. if (S_ISDIR(inode->i_mode))
  809. /* drop any readdir cache as it could easily be old */
  810. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  811. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  812. drop_nlink(inode);
  813. nfs_complete_unlink(dentry, inode);
  814. }
  815. iput(inode);
  816. }
  817. const struct dentry_operations nfs_dentry_operations = {
  818. .d_revalidate = nfs_lookup_revalidate,
  819. .d_delete = nfs_dentry_delete,
  820. .d_iput = nfs_dentry_iput,
  821. };
  822. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  823. {
  824. struct dentry *res;
  825. struct dentry *parent;
  826. struct inode *inode = NULL;
  827. int error;
  828. struct nfs_fh fhandle;
  829. struct nfs_fattr fattr;
  830. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  831. dentry->d_parent->d_name.name, dentry->d_name.name);
  832. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  833. res = ERR_PTR(-ENAMETOOLONG);
  834. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  835. goto out;
  836. res = ERR_PTR(-ENOMEM);
  837. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  838. /*
  839. * If we're doing an exclusive create, optimize away the lookup
  840. * but don't hash the dentry.
  841. */
  842. if (nfs_is_exclusive_create(dir, nd)) {
  843. d_instantiate(dentry, NULL);
  844. res = NULL;
  845. goto out;
  846. }
  847. parent = dentry->d_parent;
  848. /* Protect against concurrent sillydeletes */
  849. nfs_block_sillyrename(parent);
  850. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, &fhandle, &fattr);
  851. if (error == -ENOENT)
  852. goto no_entry;
  853. if (error < 0) {
  854. res = ERR_PTR(error);
  855. goto out_unblock_sillyrename;
  856. }
  857. inode = nfs_fhget(dentry->d_sb, &fhandle, &fattr);
  858. res = (struct dentry *)inode;
  859. if (IS_ERR(res))
  860. goto out_unblock_sillyrename;
  861. no_entry:
  862. res = d_materialise_unique(dentry, inode);
  863. if (res != NULL) {
  864. if (IS_ERR(res))
  865. goto out_unblock_sillyrename;
  866. dentry = res;
  867. }
  868. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  869. out_unblock_sillyrename:
  870. nfs_unblock_sillyrename(parent);
  871. out:
  872. return res;
  873. }
  874. #ifdef CONFIG_NFS_V4
  875. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  876. const struct dentry_operations nfs4_dentry_operations = {
  877. .d_revalidate = nfs_open_revalidate,
  878. .d_delete = nfs_dentry_delete,
  879. .d_iput = nfs_dentry_iput,
  880. };
  881. /*
  882. * Use intent information to determine whether we need to substitute
  883. * the NFSv4-style stateful OPEN for the LOOKUP call
  884. */
  885. static int is_atomic_open(struct nameidata *nd)
  886. {
  887. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  888. return 0;
  889. /* NFS does not (yet) have a stateful open for directories */
  890. if (nd->flags & LOOKUP_DIRECTORY)
  891. return 0;
  892. /* Are we trying to write to a read only partition? */
  893. if (__mnt_is_readonly(nd->path.mnt) &&
  894. (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  895. return 0;
  896. return 1;
  897. }
  898. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  899. {
  900. struct dentry *res = NULL;
  901. int error;
  902. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  903. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  904. /* Check that we are indeed trying to open this file */
  905. if (!is_atomic_open(nd))
  906. goto no_open;
  907. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  908. res = ERR_PTR(-ENAMETOOLONG);
  909. goto out;
  910. }
  911. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  912. /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
  913. * the dentry. */
  914. if (nd->flags & LOOKUP_EXCL) {
  915. d_instantiate(dentry, NULL);
  916. goto out;
  917. }
  918. /* Open the file on the server */
  919. res = nfs4_atomic_open(dir, dentry, nd);
  920. if (IS_ERR(res)) {
  921. error = PTR_ERR(res);
  922. switch (error) {
  923. /* Make a negative dentry */
  924. case -ENOENT:
  925. res = NULL;
  926. goto out;
  927. /* This turned out not to be a regular file */
  928. case -EISDIR:
  929. case -ENOTDIR:
  930. goto no_open;
  931. case -ELOOP:
  932. if (!(nd->intent.open.flags & O_NOFOLLOW))
  933. goto no_open;
  934. /* case -EINVAL: */
  935. default:
  936. goto out;
  937. }
  938. } else if (res != NULL)
  939. dentry = res;
  940. out:
  941. return res;
  942. no_open:
  943. return nfs_lookup(dir, dentry, nd);
  944. }
  945. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  946. {
  947. struct dentry *parent = NULL;
  948. struct inode *inode = dentry->d_inode;
  949. struct inode *dir;
  950. int openflags, ret = 0;
  951. if (!is_atomic_open(nd))
  952. goto no_open;
  953. parent = dget_parent(dentry);
  954. dir = parent->d_inode;
  955. /* We can't create new files in nfs_open_revalidate(), so we
  956. * optimize away revalidation of negative dentries.
  957. */
  958. if (inode == NULL) {
  959. if (!nfs_neg_need_reval(dir, dentry, nd))
  960. ret = 1;
  961. goto out;
  962. }
  963. /* NFS only supports OPEN on regular files */
  964. if (!S_ISREG(inode->i_mode))
  965. goto no_open_dput;
  966. openflags = nd->intent.open.flags;
  967. /* We cannot do exclusive creation on a positive dentry */
  968. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  969. goto no_open_dput;
  970. /* We can't create new files, or truncate existing ones here */
  971. openflags &= ~(O_CREAT|O_TRUNC);
  972. /*
  973. * Note: we're not holding inode->i_mutex and so may be racing with
  974. * operations that change the directory. We therefore save the
  975. * change attribute *before* we do the RPC call.
  976. */
  977. ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
  978. out:
  979. dput(parent);
  980. if (!ret)
  981. d_drop(dentry);
  982. return ret;
  983. no_open_dput:
  984. dput(parent);
  985. no_open:
  986. return nfs_lookup_revalidate(dentry, nd);
  987. }
  988. #endif /* CONFIG_NFSV4 */
  989. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  990. {
  991. struct dentry *parent = desc->file->f_path.dentry;
  992. struct inode *dir = parent->d_inode;
  993. struct nfs_entry *entry = desc->entry;
  994. struct dentry *dentry, *alias;
  995. struct qstr name = {
  996. .name = entry->name,
  997. .len = entry->len,
  998. };
  999. struct inode *inode;
  1000. unsigned long verf = nfs_save_change_attribute(dir);
  1001. switch (name.len) {
  1002. case 2:
  1003. if (name.name[0] == '.' && name.name[1] == '.')
  1004. return dget_parent(parent);
  1005. break;
  1006. case 1:
  1007. if (name.name[0] == '.')
  1008. return dget(parent);
  1009. }
  1010. spin_lock(&dir->i_lock);
  1011. if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) {
  1012. spin_unlock(&dir->i_lock);
  1013. return NULL;
  1014. }
  1015. spin_unlock(&dir->i_lock);
  1016. name.hash = full_name_hash(name.name, name.len);
  1017. dentry = d_lookup(parent, &name);
  1018. if (dentry != NULL) {
  1019. /* Is this a positive dentry that matches the readdir info? */
  1020. if (dentry->d_inode != NULL &&
  1021. (NFS_FILEID(dentry->d_inode) == entry->ino ||
  1022. d_mountpoint(dentry))) {
  1023. if (!desc->plus || entry->fh->size == 0)
  1024. return dentry;
  1025. if (nfs_compare_fh(NFS_FH(dentry->d_inode),
  1026. entry->fh) == 0)
  1027. goto out_renew;
  1028. }
  1029. /* No, so d_drop to allow one to be created */
  1030. d_drop(dentry);
  1031. dput(dentry);
  1032. }
  1033. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  1034. return NULL;
  1035. if (name.len > NFS_SERVER(dir)->namelen)
  1036. return NULL;
  1037. /* Note: caller is already holding the dir->i_mutex! */
  1038. dentry = d_alloc(parent, &name);
  1039. if (dentry == NULL)
  1040. return NULL;
  1041. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  1042. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  1043. if (IS_ERR(inode)) {
  1044. dput(dentry);
  1045. return NULL;
  1046. }
  1047. alias = d_materialise_unique(dentry, inode);
  1048. if (alias != NULL) {
  1049. dput(dentry);
  1050. if (IS_ERR(alias))
  1051. return NULL;
  1052. dentry = alias;
  1053. }
  1054. out_renew:
  1055. nfs_set_verifier(dentry, verf);
  1056. return dentry;
  1057. }
  1058. /*
  1059. * Code common to create, mkdir, and mknod.
  1060. */
  1061. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1062. struct nfs_fattr *fattr)
  1063. {
  1064. struct dentry *parent = dget_parent(dentry);
  1065. struct inode *dir = parent->d_inode;
  1066. struct inode *inode;
  1067. int error = -EACCES;
  1068. d_drop(dentry);
  1069. /* We may have been initialized further down */
  1070. if (dentry->d_inode)
  1071. goto out;
  1072. if (fhandle->size == 0) {
  1073. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1074. if (error)
  1075. goto out_error;
  1076. }
  1077. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1078. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1079. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1080. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1081. if (error < 0)
  1082. goto out_error;
  1083. }
  1084. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1085. error = PTR_ERR(inode);
  1086. if (IS_ERR(inode))
  1087. goto out_error;
  1088. d_add(dentry, inode);
  1089. out:
  1090. dput(parent);
  1091. return 0;
  1092. out_error:
  1093. nfs_mark_for_revalidate(dir);
  1094. dput(parent);
  1095. return error;
  1096. }
  1097. /*
  1098. * Following a failed create operation, we drop the dentry rather
  1099. * than retain a negative dentry. This avoids a problem in the event
  1100. * that the operation succeeded on the server, but an error in the
  1101. * reply path made it appear to have failed.
  1102. */
  1103. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1104. struct nameidata *nd)
  1105. {
  1106. struct iattr attr;
  1107. int error;
  1108. int open_flags = 0;
  1109. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1110. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1111. attr.ia_mode = mode;
  1112. attr.ia_valid = ATTR_MODE;
  1113. if ((nd->flags & LOOKUP_CREATE) != 0)
  1114. open_flags = nd->intent.open.flags;
  1115. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
  1116. if (error != 0)
  1117. goto out_err;
  1118. return 0;
  1119. out_err:
  1120. d_drop(dentry);
  1121. return error;
  1122. }
  1123. /*
  1124. * See comments for nfs_proc_create regarding failed operations.
  1125. */
  1126. static int
  1127. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1128. {
  1129. struct iattr attr;
  1130. int status;
  1131. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1132. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1133. if (!new_valid_dev(rdev))
  1134. return -EINVAL;
  1135. attr.ia_mode = mode;
  1136. attr.ia_valid = ATTR_MODE;
  1137. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1138. if (status != 0)
  1139. goto out_err;
  1140. return 0;
  1141. out_err:
  1142. d_drop(dentry);
  1143. return status;
  1144. }
  1145. /*
  1146. * See comments for nfs_proc_create regarding failed operations.
  1147. */
  1148. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1149. {
  1150. struct iattr attr;
  1151. int error;
  1152. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1153. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1154. attr.ia_valid = ATTR_MODE;
  1155. attr.ia_mode = mode | S_IFDIR;
  1156. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1157. if (error != 0)
  1158. goto out_err;
  1159. return 0;
  1160. out_err:
  1161. d_drop(dentry);
  1162. return error;
  1163. }
  1164. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1165. {
  1166. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1167. d_delete(dentry);
  1168. }
  1169. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1170. {
  1171. int error;
  1172. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1173. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1174. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1175. /* Ensure the VFS deletes this inode */
  1176. if (error == 0 && dentry->d_inode != NULL)
  1177. clear_nlink(dentry->d_inode);
  1178. else if (error == -ENOENT)
  1179. nfs_dentry_handle_enoent(dentry);
  1180. return error;
  1181. }
  1182. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1183. {
  1184. static unsigned int sillycounter;
  1185. const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
  1186. const int countersize = sizeof(sillycounter)*2;
  1187. const int slen = sizeof(".nfs")+fileidsize+countersize-1;
  1188. char silly[slen+1];
  1189. struct qstr qsilly;
  1190. struct dentry *sdentry;
  1191. int error = -EIO;
  1192. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1193. dentry->d_parent->d_name.name, dentry->d_name.name,
  1194. atomic_read(&dentry->d_count));
  1195. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  1196. /*
  1197. * We don't allow a dentry to be silly-renamed twice.
  1198. */
  1199. error = -EBUSY;
  1200. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1201. goto out;
  1202. sprintf(silly, ".nfs%*.*Lx",
  1203. fileidsize, fileidsize,
  1204. (unsigned long long)NFS_FILEID(dentry->d_inode));
  1205. /* Return delegation in anticipation of the rename */
  1206. nfs_inode_return_delegation(dentry->d_inode);
  1207. sdentry = NULL;
  1208. do {
  1209. char *suffix = silly + slen - countersize;
  1210. dput(sdentry);
  1211. sillycounter++;
  1212. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1213. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  1214. dentry->d_name.name, silly);
  1215. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1216. /*
  1217. * N.B. Better to return EBUSY here ... it could be
  1218. * dangerous to delete the file while it's in use.
  1219. */
  1220. if (IS_ERR(sdentry))
  1221. goto out;
  1222. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1223. qsilly.name = silly;
  1224. qsilly.len = strlen(silly);
  1225. if (dentry->d_inode) {
  1226. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1227. dir, &qsilly);
  1228. nfs_mark_for_revalidate(dentry->d_inode);
  1229. } else
  1230. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1231. dir, &qsilly);
  1232. if (!error) {
  1233. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1234. d_move(dentry, sdentry);
  1235. error = nfs_async_unlink(dir, dentry);
  1236. /* If we return 0 we don't unlink */
  1237. }
  1238. dput(sdentry);
  1239. out:
  1240. return error;
  1241. }
  1242. /*
  1243. * Remove a file after making sure there are no pending writes,
  1244. * and after checking that the file has only one user.
  1245. *
  1246. * We invalidate the attribute cache and free the inode prior to the operation
  1247. * to avoid possible races if the server reuses the inode.
  1248. */
  1249. static int nfs_safe_remove(struct dentry *dentry)
  1250. {
  1251. struct inode *dir = dentry->d_parent->d_inode;
  1252. struct inode *inode = dentry->d_inode;
  1253. int error = -EBUSY;
  1254. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1255. dentry->d_parent->d_name.name, dentry->d_name.name);
  1256. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1257. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1258. error = 0;
  1259. goto out;
  1260. }
  1261. if (inode != NULL) {
  1262. nfs_inode_return_delegation(inode);
  1263. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1264. /* The VFS may want to delete this inode */
  1265. if (error == 0)
  1266. nfs_drop_nlink(inode);
  1267. nfs_mark_for_revalidate(inode);
  1268. } else
  1269. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1270. if (error == -ENOENT)
  1271. nfs_dentry_handle_enoent(dentry);
  1272. out:
  1273. return error;
  1274. }
  1275. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1276. * belongs to an active ".nfs..." file and we return -EBUSY.
  1277. *
  1278. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1279. */
  1280. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1281. {
  1282. int error;
  1283. int need_rehash = 0;
  1284. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1285. dir->i_ino, dentry->d_name.name);
  1286. spin_lock(&dcache_lock);
  1287. spin_lock(&dentry->d_lock);
  1288. if (atomic_read(&dentry->d_count) > 1) {
  1289. spin_unlock(&dentry->d_lock);
  1290. spin_unlock(&dcache_lock);
  1291. /* Start asynchronous writeout of the inode */
  1292. write_inode_now(dentry->d_inode, 0);
  1293. error = nfs_sillyrename(dir, dentry);
  1294. return error;
  1295. }
  1296. if (!d_unhashed(dentry)) {
  1297. __d_drop(dentry);
  1298. need_rehash = 1;
  1299. }
  1300. spin_unlock(&dentry->d_lock);
  1301. spin_unlock(&dcache_lock);
  1302. error = nfs_safe_remove(dentry);
  1303. if (!error || error == -ENOENT) {
  1304. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1305. } else if (need_rehash)
  1306. d_rehash(dentry);
  1307. return error;
  1308. }
  1309. /*
  1310. * To create a symbolic link, most file systems instantiate a new inode,
  1311. * add a page to it containing the path, then write it out to the disk
  1312. * using prepare_write/commit_write.
  1313. *
  1314. * Unfortunately the NFS client can't create the in-core inode first
  1315. * because it needs a file handle to create an in-core inode (see
  1316. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1317. * symlink request has completed on the server.
  1318. *
  1319. * So instead we allocate a raw page, copy the symname into it, then do
  1320. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1321. * now have a new file handle and can instantiate an in-core NFS inode
  1322. * and move the raw page into its mapping.
  1323. */
  1324. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1325. {
  1326. struct pagevec lru_pvec;
  1327. struct page *page;
  1328. char *kaddr;
  1329. struct iattr attr;
  1330. unsigned int pathlen = strlen(symname);
  1331. int error;
  1332. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1333. dir->i_ino, dentry->d_name.name, symname);
  1334. if (pathlen > PAGE_SIZE)
  1335. return -ENAMETOOLONG;
  1336. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1337. attr.ia_valid = ATTR_MODE;
  1338. page = alloc_page(GFP_HIGHUSER);
  1339. if (!page)
  1340. return -ENOMEM;
  1341. kaddr = kmap_atomic(page, KM_USER0);
  1342. memcpy(kaddr, symname, pathlen);
  1343. if (pathlen < PAGE_SIZE)
  1344. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1345. kunmap_atomic(kaddr, KM_USER0);
  1346. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1347. if (error != 0) {
  1348. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1349. dir->i_sb->s_id, dir->i_ino,
  1350. dentry->d_name.name, symname, error);
  1351. d_drop(dentry);
  1352. __free_page(page);
  1353. return error;
  1354. }
  1355. /*
  1356. * No big deal if we can't add this page to the page cache here.
  1357. * READLINK will get the missing page from the server if needed.
  1358. */
  1359. pagevec_init(&lru_pvec, 0);
  1360. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1361. GFP_KERNEL)) {
  1362. pagevec_add(&lru_pvec, page);
  1363. pagevec_lru_add_file(&lru_pvec);
  1364. SetPageUptodate(page);
  1365. unlock_page(page);
  1366. } else
  1367. __free_page(page);
  1368. return 0;
  1369. }
  1370. static int
  1371. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1372. {
  1373. struct inode *inode = old_dentry->d_inode;
  1374. int error;
  1375. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1376. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1377. dentry->d_parent->d_name.name, dentry->d_name.name);
  1378. d_drop(dentry);
  1379. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1380. if (error == 0) {
  1381. atomic_inc(&inode->i_count);
  1382. d_add(dentry, inode);
  1383. }
  1384. return error;
  1385. }
  1386. /*
  1387. * RENAME
  1388. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1389. * different file handle for the same inode after a rename (e.g. when
  1390. * moving to a different directory). A fail-safe method to do so would
  1391. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1392. * rename the old file using the sillyrename stuff. This way, the original
  1393. * file in old_dir will go away when the last process iput()s the inode.
  1394. *
  1395. * FIXED.
  1396. *
  1397. * It actually works quite well. One needs to have the possibility for
  1398. * at least one ".nfs..." file in each directory the file ever gets
  1399. * moved or linked to which happens automagically with the new
  1400. * implementation that only depends on the dcache stuff instead of
  1401. * using the inode layer
  1402. *
  1403. * Unfortunately, things are a little more complicated than indicated
  1404. * above. For a cross-directory move, we want to make sure we can get
  1405. * rid of the old inode after the operation. This means there must be
  1406. * no pending writes (if it's a file), and the use count must be 1.
  1407. * If these conditions are met, we can drop the dentries before doing
  1408. * the rename.
  1409. */
  1410. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1411. struct inode *new_dir, struct dentry *new_dentry)
  1412. {
  1413. struct inode *old_inode = old_dentry->d_inode;
  1414. struct inode *new_inode = new_dentry->d_inode;
  1415. struct dentry *dentry = NULL, *rehash = NULL;
  1416. int error = -EBUSY;
  1417. /*
  1418. * To prevent any new references to the target during the rename,
  1419. * we unhash the dentry and free the inode in advance.
  1420. */
  1421. if (!d_unhashed(new_dentry)) {
  1422. d_drop(new_dentry);
  1423. rehash = new_dentry;
  1424. }
  1425. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1426. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1427. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1428. atomic_read(&new_dentry->d_count));
  1429. /*
  1430. * First check whether the target is busy ... we can't
  1431. * safely do _any_ rename if the target is in use.
  1432. *
  1433. * For files, make a copy of the dentry and then do a
  1434. * silly-rename. If the silly-rename succeeds, the
  1435. * copied dentry is hashed and becomes the new target.
  1436. */
  1437. if (!new_inode)
  1438. goto go_ahead;
  1439. if (S_ISDIR(new_inode->i_mode)) {
  1440. error = -EISDIR;
  1441. if (!S_ISDIR(old_inode->i_mode))
  1442. goto out;
  1443. } else if (atomic_read(&new_dentry->d_count) > 2) {
  1444. int err;
  1445. /* copy the target dentry's name */
  1446. dentry = d_alloc(new_dentry->d_parent,
  1447. &new_dentry->d_name);
  1448. if (!dentry)
  1449. goto out;
  1450. /* silly-rename the existing target ... */
  1451. err = nfs_sillyrename(new_dir, new_dentry);
  1452. if (!err) {
  1453. new_dentry = rehash = dentry;
  1454. new_inode = NULL;
  1455. /* instantiate the replacement target */
  1456. d_instantiate(new_dentry, NULL);
  1457. } else if (atomic_read(&new_dentry->d_count) > 1)
  1458. /* dentry still busy? */
  1459. goto out;
  1460. }
  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. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1474. new_dir, &new_dentry->d_name);
  1475. nfs_mark_for_revalidate(old_inode);
  1476. out:
  1477. if (rehash)
  1478. d_rehash(rehash);
  1479. if (!error) {
  1480. if (new_inode != NULL)
  1481. nfs_drop_nlink(new_inode);
  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 (!nfs_have_delegation(inode, FMODE_READ) &&
  1614. !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1615. goto out_stale;
  1616. res->jiffies = cache->jiffies;
  1617. res->cred = cache->cred;
  1618. res->mask = cache->mask;
  1619. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1620. err = 0;
  1621. out:
  1622. spin_unlock(&inode->i_lock);
  1623. return err;
  1624. out_stale:
  1625. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1626. list_del(&cache->lru);
  1627. spin_unlock(&inode->i_lock);
  1628. nfs_access_free_entry(cache);
  1629. return -ENOENT;
  1630. out_zap:
  1631. /* This will release the spinlock */
  1632. __nfs_access_zap_cache(inode);
  1633. return -ENOENT;
  1634. }
  1635. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1636. {
  1637. struct nfs_inode *nfsi = NFS_I(inode);
  1638. struct rb_root *root_node = &nfsi->access_cache;
  1639. struct rb_node **p = &root_node->rb_node;
  1640. struct rb_node *parent = NULL;
  1641. struct nfs_access_entry *entry;
  1642. spin_lock(&inode->i_lock);
  1643. while (*p != NULL) {
  1644. parent = *p;
  1645. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1646. if (set->cred < entry->cred)
  1647. p = &parent->rb_left;
  1648. else if (set->cred > entry->cred)
  1649. p = &parent->rb_right;
  1650. else
  1651. goto found;
  1652. }
  1653. rb_link_node(&set->rb_node, parent, p);
  1654. rb_insert_color(&set->rb_node, root_node);
  1655. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1656. spin_unlock(&inode->i_lock);
  1657. return;
  1658. found:
  1659. rb_replace_node(parent, &set->rb_node, root_node);
  1660. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1661. list_del(&entry->lru);
  1662. spin_unlock(&inode->i_lock);
  1663. nfs_access_free_entry(entry);
  1664. }
  1665. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1666. {
  1667. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1668. if (cache == NULL)
  1669. return;
  1670. RB_CLEAR_NODE(&cache->rb_node);
  1671. cache->jiffies = set->jiffies;
  1672. cache->cred = get_rpccred(set->cred);
  1673. cache->mask = set->mask;
  1674. nfs_access_add_rbtree(inode, cache);
  1675. /* Update accounting */
  1676. smp_mb__before_atomic_inc();
  1677. atomic_long_inc(&nfs_access_nr_entries);
  1678. smp_mb__after_atomic_inc();
  1679. /* Add inode to global LRU list */
  1680. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1681. spin_lock(&nfs_access_lru_lock);
  1682. list_add_tail(&NFS_I(inode)->access_cache_inode_lru, &nfs_access_lru_list);
  1683. spin_unlock(&nfs_access_lru_lock);
  1684. }
  1685. }
  1686. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1687. {
  1688. struct nfs_access_entry cache;
  1689. int status;
  1690. status = nfs_access_get_cached(inode, cred, &cache);
  1691. if (status == 0)
  1692. goto out;
  1693. /* Be clever: ask server to check for all possible rights */
  1694. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1695. cache.cred = cred;
  1696. cache.jiffies = jiffies;
  1697. status = NFS_PROTO(inode)->access(inode, &cache);
  1698. if (status != 0) {
  1699. if (status == -ESTALE) {
  1700. nfs_zap_caches(inode);
  1701. if (!S_ISDIR(inode->i_mode))
  1702. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1703. }
  1704. return status;
  1705. }
  1706. nfs_access_add_cache(inode, &cache);
  1707. out:
  1708. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1709. return 0;
  1710. return -EACCES;
  1711. }
  1712. static int nfs_open_permission_mask(int openflags)
  1713. {
  1714. int mask = 0;
  1715. if (openflags & FMODE_READ)
  1716. mask |= MAY_READ;
  1717. if (openflags & FMODE_WRITE)
  1718. mask |= MAY_WRITE;
  1719. if (openflags & FMODE_EXEC)
  1720. mask |= MAY_EXEC;
  1721. return mask;
  1722. }
  1723. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  1724. {
  1725. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  1726. }
  1727. int nfs_permission(struct inode *inode, int mask)
  1728. {
  1729. struct rpc_cred *cred;
  1730. int res = 0;
  1731. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1732. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1733. goto out;
  1734. /* Is this sys_access() ? */
  1735. if (mask & MAY_ACCESS)
  1736. goto force_lookup;
  1737. switch (inode->i_mode & S_IFMT) {
  1738. case S_IFLNK:
  1739. goto out;
  1740. case S_IFREG:
  1741. /* NFSv4 has atomic_open... */
  1742. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1743. && (mask & MAY_OPEN)
  1744. && !(mask & MAY_EXEC))
  1745. goto out;
  1746. break;
  1747. case S_IFDIR:
  1748. /*
  1749. * Optimize away all write operations, since the server
  1750. * will check permissions when we perform the op.
  1751. */
  1752. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1753. goto out;
  1754. }
  1755. force_lookup:
  1756. if (!NFS_PROTO(inode)->access)
  1757. goto out_notsup;
  1758. cred = rpc_lookup_cred();
  1759. if (!IS_ERR(cred)) {
  1760. res = nfs_do_access(inode, cred, mask);
  1761. put_rpccred(cred);
  1762. } else
  1763. res = PTR_ERR(cred);
  1764. out:
  1765. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  1766. res = -EACCES;
  1767. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  1768. inode->i_sb->s_id, inode->i_ino, mask, res);
  1769. return res;
  1770. out_notsup:
  1771. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1772. if (res == 0)
  1773. res = generic_permission(inode, mask, NULL);
  1774. goto out;
  1775. }
  1776. /*
  1777. * Local variables:
  1778. * version-control: t
  1779. * kept-new-versions: 5
  1780. * End:
  1781. */