dir.c 55 KB

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