dir.c 50 KB

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