dir.c 57 KB

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