dir.c 59 KB

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