dir.c 51 KB

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