dir.c 52 KB

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