namei.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/namei.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/personality.h>
  23. #include <linux/security.h>
  24. #include <linux/ima.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/capability.h>
  29. #include <linux/file.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/device_cgroup.h>
  32. #include <linux/fs_struct.h>
  33. #include <linux/posix_acl.h>
  34. #include <asm/uaccess.h>
  35. #include "internal.h"
  36. #include "mount.h"
  37. /* [Feb-1997 T. Schoebel-Theuer]
  38. * Fundamental changes in the pathname lookup mechanisms (namei)
  39. * were necessary because of omirr. The reason is that omirr needs
  40. * to know the _real_ pathname, not the user-supplied one, in case
  41. * of symlinks (and also when transname replacements occur).
  42. *
  43. * The new code replaces the old recursive symlink resolution with
  44. * an iterative one (in case of non-nested symlink chains). It does
  45. * this with calls to <fs>_follow_link().
  46. * As a side effect, dir_namei(), _namei() and follow_link() are now
  47. * replaced with a single function lookup_dentry() that can handle all
  48. * the special cases of the former code.
  49. *
  50. * With the new dcache, the pathname is stored at each inode, at least as
  51. * long as the refcount of the inode is positive. As a side effect, the
  52. * size of the dcache depends on the inode cache and thus is dynamic.
  53. *
  54. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  55. * resolution to correspond with current state of the code.
  56. *
  57. * Note that the symlink resolution is not *completely* iterative.
  58. * There is still a significant amount of tail- and mid- recursion in
  59. * the algorithm. Also, note that <fs>_readlink() is not used in
  60. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  61. * may return different results than <fs>_follow_link(). Many virtual
  62. * filesystems (including /proc) exhibit this behavior.
  63. */
  64. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  65. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  66. * and the name already exists in form of a symlink, try to create the new
  67. * name indicated by the symlink. The old code always complained that the
  68. * name already exists, due to not following the symlink even if its target
  69. * is nonexistent. The new semantics affects also mknod() and link() when
  70. * the name is a symlink pointing to a non-existent name.
  71. *
  72. * I don't know which semantics is the right one, since I have no access
  73. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  74. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  75. * "old" one. Personally, I think the new semantics is much more logical.
  76. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  77. * file does succeed in both HP-UX and SunOs, but not in Solaris
  78. * and in the old Linux semantics.
  79. */
  80. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  81. * semantics. See the comments in "open_namei" and "do_link" below.
  82. *
  83. * [10-Sep-98 Alan Modra] Another symlink change.
  84. */
  85. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  86. * inside the path - always follow.
  87. * in the last component in creation/removal/renaming - never follow.
  88. * if LOOKUP_FOLLOW passed - follow.
  89. * if the pathname has trailing slashes - follow.
  90. * otherwise - don't follow.
  91. * (applied in that order).
  92. *
  93. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  94. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  95. * During the 2.4 we need to fix the userland stuff depending on it -
  96. * hopefully we will be able to get rid of that wart in 2.5. So far only
  97. * XEmacs seems to be relying on it...
  98. */
  99. /*
  100. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  101. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  102. * any extra contention...
  103. */
  104. /* In order to reduce some races, while at the same time doing additional
  105. * checking and hopefully speeding things up, we copy filenames to the
  106. * kernel data space before using them..
  107. *
  108. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  109. * PATH_MAX includes the nul terminator --RR.
  110. */
  111. static char *getname_flags(const char __user *filename, int flags, int *empty)
  112. {
  113. char *result = __getname(), *err;
  114. int len;
  115. if (unlikely(!result))
  116. return ERR_PTR(-ENOMEM);
  117. len = strncpy_from_user(result, filename, PATH_MAX);
  118. err = ERR_PTR(len);
  119. if (unlikely(len < 0))
  120. goto error;
  121. /* The empty path is special. */
  122. if (unlikely(!len)) {
  123. if (empty)
  124. *empty = 1;
  125. err = ERR_PTR(-ENOENT);
  126. if (!(flags & LOOKUP_EMPTY))
  127. goto error;
  128. }
  129. err = ERR_PTR(-ENAMETOOLONG);
  130. if (likely(len < PATH_MAX)) {
  131. audit_getname(result);
  132. return result;
  133. }
  134. error:
  135. __putname(result);
  136. return err;
  137. }
  138. char *getname(const char __user * filename)
  139. {
  140. return getname_flags(filename, 0, NULL);
  141. }
  142. #ifdef CONFIG_AUDITSYSCALL
  143. void putname(const char *name)
  144. {
  145. if (unlikely(!audit_dummy_context()))
  146. audit_putname(name);
  147. else
  148. __putname(name);
  149. }
  150. EXPORT_SYMBOL(putname);
  151. #endif
  152. static int check_acl(struct inode *inode, int mask)
  153. {
  154. #ifdef CONFIG_FS_POSIX_ACL
  155. struct posix_acl *acl;
  156. if (mask & MAY_NOT_BLOCK) {
  157. acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  158. if (!acl)
  159. return -EAGAIN;
  160. /* no ->get_acl() calls in RCU mode... */
  161. if (acl == ACL_NOT_CACHED)
  162. return -ECHILD;
  163. return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
  164. }
  165. acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
  166. /*
  167. * A filesystem can force a ACL callback by just never filling the
  168. * ACL cache. But normally you'd fill the cache either at inode
  169. * instantiation time, or on the first ->get_acl call.
  170. *
  171. * If the filesystem doesn't have a get_acl() function at all, we'll
  172. * just create the negative cache entry.
  173. */
  174. if (acl == ACL_NOT_CACHED) {
  175. if (inode->i_op->get_acl) {
  176. acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
  177. if (IS_ERR(acl))
  178. return PTR_ERR(acl);
  179. } else {
  180. set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
  181. return -EAGAIN;
  182. }
  183. }
  184. if (acl) {
  185. int error = posix_acl_permission(inode, acl, mask);
  186. posix_acl_release(acl);
  187. return error;
  188. }
  189. #endif
  190. return -EAGAIN;
  191. }
  192. /*
  193. * This does the basic permission checking
  194. */
  195. static int acl_permission_check(struct inode *inode, int mask)
  196. {
  197. unsigned int mode = inode->i_mode;
  198. if (likely(uid_eq(current_fsuid(), inode->i_uid)))
  199. mode >>= 6;
  200. else {
  201. if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
  202. int error = check_acl(inode, mask);
  203. if (error != -EAGAIN)
  204. return error;
  205. }
  206. if (in_group_p(inode->i_gid))
  207. mode >>= 3;
  208. }
  209. /*
  210. * If the DACs are ok we don't need any capability check.
  211. */
  212. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  213. return 0;
  214. return -EACCES;
  215. }
  216. /**
  217. * generic_permission - check for access rights on a Posix-like filesystem
  218. * @inode: inode to check access rights for
  219. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
  220. *
  221. * Used to check for read/write/execute permissions on a file.
  222. * We use "fsuid" for this, letting us set arbitrary permissions
  223. * for filesystem access without changing the "normal" uids which
  224. * are used for other things.
  225. *
  226. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  227. * request cannot be satisfied (eg. requires blocking or too much complexity).
  228. * It would then be called again in ref-walk mode.
  229. */
  230. int generic_permission(struct inode *inode, int mask)
  231. {
  232. int ret;
  233. /*
  234. * Do the basic permission checks.
  235. */
  236. ret = acl_permission_check(inode, mask);
  237. if (ret != -EACCES)
  238. return ret;
  239. if (S_ISDIR(inode->i_mode)) {
  240. /* DACs are overridable for directories */
  241. if (inode_capable(inode, CAP_DAC_OVERRIDE))
  242. return 0;
  243. if (!(mask & MAY_WRITE))
  244. if (inode_capable(inode, CAP_DAC_READ_SEARCH))
  245. return 0;
  246. return -EACCES;
  247. }
  248. /*
  249. * Read/write DACs are always overridable.
  250. * Executable DACs are overridable when there is
  251. * at least one exec bit set.
  252. */
  253. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  254. if (inode_capable(inode, CAP_DAC_OVERRIDE))
  255. return 0;
  256. /*
  257. * Searching includes executable on directories, else just read.
  258. */
  259. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  260. if (mask == MAY_READ)
  261. if (inode_capable(inode, CAP_DAC_READ_SEARCH))
  262. return 0;
  263. return -EACCES;
  264. }
  265. /*
  266. * We _really_ want to just do "generic_permission()" without
  267. * even looking at the inode->i_op values. So we keep a cache
  268. * flag in inode->i_opflags, that says "this has not special
  269. * permission function, use the fast case".
  270. */
  271. static inline int do_inode_permission(struct inode *inode, int mask)
  272. {
  273. if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  274. if (likely(inode->i_op->permission))
  275. return inode->i_op->permission(inode, mask);
  276. /* This gets set once for the inode lifetime */
  277. spin_lock(&inode->i_lock);
  278. inode->i_opflags |= IOP_FASTPERM;
  279. spin_unlock(&inode->i_lock);
  280. }
  281. return generic_permission(inode, mask);
  282. }
  283. /**
  284. * __inode_permission - Check for access rights to a given inode
  285. * @inode: Inode to check permission on
  286. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  287. *
  288. * Check for read/write/execute permissions on an inode.
  289. *
  290. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  291. *
  292. * This does not check for a read-only file system. You probably want
  293. * inode_permission().
  294. */
  295. int __inode_permission(struct inode *inode, int mask)
  296. {
  297. int retval;
  298. if (unlikely(mask & MAY_WRITE)) {
  299. /*
  300. * Nobody gets write access to an immutable file.
  301. */
  302. if (IS_IMMUTABLE(inode))
  303. return -EACCES;
  304. }
  305. retval = do_inode_permission(inode, mask);
  306. if (retval)
  307. return retval;
  308. retval = devcgroup_inode_permission(inode, mask);
  309. if (retval)
  310. return retval;
  311. return security_inode_permission(inode, mask);
  312. }
  313. /**
  314. * sb_permission - Check superblock-level permissions
  315. * @sb: Superblock of inode to check permission on
  316. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  317. *
  318. * Separate out file-system wide checks from inode-specific permission checks.
  319. */
  320. static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
  321. {
  322. if (unlikely(mask & MAY_WRITE)) {
  323. umode_t mode = inode->i_mode;
  324. /* Nobody gets write access to a read-only fs. */
  325. if ((sb->s_flags & MS_RDONLY) &&
  326. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  327. return -EROFS;
  328. }
  329. return 0;
  330. }
  331. /**
  332. * inode_permission - Check for access rights to a given inode
  333. * @inode: Inode to check permission on
  334. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  335. *
  336. * Check for read/write/execute permissions on an inode. We use fs[ug]id for
  337. * this, letting us set arbitrary permissions for filesystem access without
  338. * changing the "normal" UIDs which are used for other things.
  339. *
  340. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  341. */
  342. int inode_permission(struct inode *inode, int mask)
  343. {
  344. int retval;
  345. retval = sb_permission(inode->i_sb, inode, mask);
  346. if (retval)
  347. return retval;
  348. return __inode_permission(inode, mask);
  349. }
  350. /**
  351. * path_get - get a reference to a path
  352. * @path: path to get the reference to
  353. *
  354. * Given a path increment the reference count to the dentry and the vfsmount.
  355. */
  356. void path_get(struct path *path)
  357. {
  358. mntget(path->mnt);
  359. dget(path->dentry);
  360. }
  361. EXPORT_SYMBOL(path_get);
  362. /**
  363. * path_put - put a reference to a path
  364. * @path: path to put the reference to
  365. *
  366. * Given a path decrement the reference count to the dentry and the vfsmount.
  367. */
  368. void path_put(struct path *path)
  369. {
  370. dput(path->dentry);
  371. mntput(path->mnt);
  372. }
  373. EXPORT_SYMBOL(path_put);
  374. /*
  375. * Path walking has 2 modes, rcu-walk and ref-walk (see
  376. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  377. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  378. * normal reference counts on dentries and vfsmounts to transition to rcu-walk
  379. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  380. * got stuck, so ref-walk may continue from there. If this is not successful
  381. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  382. * to restart the path walk from the beginning in ref-walk mode.
  383. */
  384. static inline void lock_rcu_walk(void)
  385. {
  386. br_read_lock(&vfsmount_lock);
  387. rcu_read_lock();
  388. }
  389. static inline void unlock_rcu_walk(void)
  390. {
  391. rcu_read_unlock();
  392. br_read_unlock(&vfsmount_lock);
  393. }
  394. /**
  395. * unlazy_walk - try to switch to ref-walk mode.
  396. * @nd: nameidata pathwalk data
  397. * @dentry: child of nd->path.dentry or NULL
  398. * Returns: 0 on success, -ECHILD on failure
  399. *
  400. * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
  401. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  402. * @nd or NULL. Must be called from rcu-walk context.
  403. */
  404. static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
  405. {
  406. struct fs_struct *fs = current->fs;
  407. struct dentry *parent = nd->path.dentry;
  408. int want_root = 0;
  409. BUG_ON(!(nd->flags & LOOKUP_RCU));
  410. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  411. want_root = 1;
  412. spin_lock(&fs->lock);
  413. if (nd->root.mnt != fs->root.mnt ||
  414. nd->root.dentry != fs->root.dentry)
  415. goto err_root;
  416. }
  417. spin_lock(&parent->d_lock);
  418. if (!dentry) {
  419. if (!__d_rcu_to_refcount(parent, nd->seq))
  420. goto err_parent;
  421. BUG_ON(nd->inode != parent->d_inode);
  422. } else {
  423. if (dentry->d_parent != parent)
  424. goto err_parent;
  425. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  426. if (!__d_rcu_to_refcount(dentry, nd->seq))
  427. goto err_child;
  428. /*
  429. * If the sequence check on the child dentry passed, then
  430. * the child has not been removed from its parent. This
  431. * means the parent dentry must be valid and able to take
  432. * a reference at this point.
  433. */
  434. BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
  435. BUG_ON(!parent->d_count);
  436. parent->d_count++;
  437. spin_unlock(&dentry->d_lock);
  438. }
  439. spin_unlock(&parent->d_lock);
  440. if (want_root) {
  441. path_get(&nd->root);
  442. spin_unlock(&fs->lock);
  443. }
  444. mntget(nd->path.mnt);
  445. unlock_rcu_walk();
  446. nd->flags &= ~LOOKUP_RCU;
  447. return 0;
  448. err_child:
  449. spin_unlock(&dentry->d_lock);
  450. err_parent:
  451. spin_unlock(&parent->d_lock);
  452. err_root:
  453. if (want_root)
  454. spin_unlock(&fs->lock);
  455. return -ECHILD;
  456. }
  457. static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
  458. {
  459. return dentry->d_op->d_revalidate(dentry, flags);
  460. }
  461. /**
  462. * complete_walk - successful completion of path walk
  463. * @nd: pointer nameidata
  464. *
  465. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  466. * Revalidate the final result, unless we'd already done that during
  467. * the path walk or the filesystem doesn't ask for it. Return 0 on
  468. * success, -error on failure. In case of failure caller does not
  469. * need to drop nd->path.
  470. */
  471. static int complete_walk(struct nameidata *nd)
  472. {
  473. struct dentry *dentry = nd->path.dentry;
  474. int status;
  475. if (nd->flags & LOOKUP_RCU) {
  476. nd->flags &= ~LOOKUP_RCU;
  477. if (!(nd->flags & LOOKUP_ROOT))
  478. nd->root.mnt = NULL;
  479. spin_lock(&dentry->d_lock);
  480. if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
  481. spin_unlock(&dentry->d_lock);
  482. unlock_rcu_walk();
  483. return -ECHILD;
  484. }
  485. BUG_ON(nd->inode != dentry->d_inode);
  486. spin_unlock(&dentry->d_lock);
  487. mntget(nd->path.mnt);
  488. unlock_rcu_walk();
  489. }
  490. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  491. return 0;
  492. if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
  493. return 0;
  494. if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
  495. return 0;
  496. /* Note: we do not d_invalidate() */
  497. status = d_revalidate(dentry, nd->flags);
  498. if (status > 0)
  499. return 0;
  500. if (!status)
  501. status = -ESTALE;
  502. path_put(&nd->path);
  503. return status;
  504. }
  505. static __always_inline void set_root(struct nameidata *nd)
  506. {
  507. if (!nd->root.mnt)
  508. get_fs_root(current->fs, &nd->root);
  509. }
  510. static int link_path_walk(const char *, struct nameidata *);
  511. static __always_inline void set_root_rcu(struct nameidata *nd)
  512. {
  513. if (!nd->root.mnt) {
  514. struct fs_struct *fs = current->fs;
  515. unsigned seq;
  516. do {
  517. seq = read_seqcount_begin(&fs->seq);
  518. nd->root = fs->root;
  519. nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  520. } while (read_seqcount_retry(&fs->seq, seq));
  521. }
  522. }
  523. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  524. {
  525. int ret;
  526. if (IS_ERR(link))
  527. goto fail;
  528. if (*link == '/') {
  529. set_root(nd);
  530. path_put(&nd->path);
  531. nd->path = nd->root;
  532. path_get(&nd->root);
  533. nd->flags |= LOOKUP_JUMPED;
  534. }
  535. nd->inode = nd->path.dentry->d_inode;
  536. ret = link_path_walk(link, nd);
  537. return ret;
  538. fail:
  539. path_put(&nd->path);
  540. return PTR_ERR(link);
  541. }
  542. static void path_put_conditional(struct path *path, struct nameidata *nd)
  543. {
  544. dput(path->dentry);
  545. if (path->mnt != nd->path.mnt)
  546. mntput(path->mnt);
  547. }
  548. static inline void path_to_nameidata(const struct path *path,
  549. struct nameidata *nd)
  550. {
  551. if (!(nd->flags & LOOKUP_RCU)) {
  552. dput(nd->path.dentry);
  553. if (nd->path.mnt != path->mnt)
  554. mntput(nd->path.mnt);
  555. }
  556. nd->path.mnt = path->mnt;
  557. nd->path.dentry = path->dentry;
  558. }
  559. /*
  560. * Helper to directly jump to a known parsed path from ->follow_link,
  561. * caller must have taken a reference to path beforehand.
  562. */
  563. void nd_jump_link(struct nameidata *nd, struct path *path)
  564. {
  565. path_put(&nd->path);
  566. nd->path = *path;
  567. nd->inode = nd->path.dentry->d_inode;
  568. nd->flags |= LOOKUP_JUMPED;
  569. BUG_ON(nd->inode->i_op->follow_link);
  570. }
  571. static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  572. {
  573. struct inode *inode = link->dentry->d_inode;
  574. if (inode->i_op->put_link)
  575. inode->i_op->put_link(link->dentry, nd, cookie);
  576. path_put(link);
  577. }
  578. static __always_inline int
  579. follow_link(struct path *link, struct nameidata *nd, void **p)
  580. {
  581. struct dentry *dentry = link->dentry;
  582. int error;
  583. char *s;
  584. BUG_ON(nd->flags & LOOKUP_RCU);
  585. if (link->mnt == nd->path.mnt)
  586. mntget(link->mnt);
  587. error = -ELOOP;
  588. if (unlikely(current->total_link_count >= 40))
  589. goto out_put_nd_path;
  590. cond_resched();
  591. current->total_link_count++;
  592. touch_atime(link);
  593. nd_set_link(nd, NULL);
  594. error = security_inode_follow_link(link->dentry, nd);
  595. if (error)
  596. goto out_put_nd_path;
  597. nd->last_type = LAST_BIND;
  598. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  599. error = PTR_ERR(*p);
  600. if (IS_ERR(*p))
  601. goto out_put_nd_path;
  602. error = 0;
  603. s = nd_get_link(nd);
  604. if (s) {
  605. error = __vfs_follow_link(nd, s);
  606. if (unlikely(error))
  607. put_link(nd, link, *p);
  608. }
  609. return error;
  610. out_put_nd_path:
  611. path_put(&nd->path);
  612. path_put(link);
  613. return error;
  614. }
  615. static int follow_up_rcu(struct path *path)
  616. {
  617. struct mount *mnt = real_mount(path->mnt);
  618. struct mount *parent;
  619. struct dentry *mountpoint;
  620. parent = mnt->mnt_parent;
  621. if (&parent->mnt == path->mnt)
  622. return 0;
  623. mountpoint = mnt->mnt_mountpoint;
  624. path->dentry = mountpoint;
  625. path->mnt = &parent->mnt;
  626. return 1;
  627. }
  628. /*
  629. * follow_up - Find the mountpoint of path's vfsmount
  630. *
  631. * Given a path, find the mountpoint of its source file system.
  632. * Replace @path with the path of the mountpoint in the parent mount.
  633. * Up is towards /.
  634. *
  635. * Return 1 if we went up a level and 0 if we were already at the
  636. * root.
  637. */
  638. int follow_up(struct path *path)
  639. {
  640. struct mount *mnt = real_mount(path->mnt);
  641. struct mount *parent;
  642. struct dentry *mountpoint;
  643. br_read_lock(&vfsmount_lock);
  644. parent = mnt->mnt_parent;
  645. if (parent == mnt) {
  646. br_read_unlock(&vfsmount_lock);
  647. return 0;
  648. }
  649. mntget(&parent->mnt);
  650. mountpoint = dget(mnt->mnt_mountpoint);
  651. br_read_unlock(&vfsmount_lock);
  652. dput(path->dentry);
  653. path->dentry = mountpoint;
  654. mntput(path->mnt);
  655. path->mnt = &parent->mnt;
  656. return 1;
  657. }
  658. /*
  659. * Perform an automount
  660. * - return -EISDIR to tell follow_managed() to stop and return the path we
  661. * were called with.
  662. */
  663. static int follow_automount(struct path *path, unsigned flags,
  664. bool *need_mntput)
  665. {
  666. struct vfsmount *mnt;
  667. int err;
  668. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  669. return -EREMOTE;
  670. /* We don't want to mount if someone's just doing a stat -
  671. * unless they're stat'ing a directory and appended a '/' to
  672. * the name.
  673. *
  674. * We do, however, want to mount if someone wants to open or
  675. * create a file of any type under the mountpoint, wants to
  676. * traverse through the mountpoint or wants to open the
  677. * mounted directory. Also, autofs may mark negative dentries
  678. * as being automount points. These will need the attentions
  679. * of the daemon to instantiate them before they can be used.
  680. */
  681. if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  682. LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
  683. path->dentry->d_inode)
  684. return -EISDIR;
  685. current->total_link_count++;
  686. if (current->total_link_count >= 40)
  687. return -ELOOP;
  688. mnt = path->dentry->d_op->d_automount(path);
  689. if (IS_ERR(mnt)) {
  690. /*
  691. * The filesystem is allowed to return -EISDIR here to indicate
  692. * it doesn't want to automount. For instance, autofs would do
  693. * this so that its userspace daemon can mount on this dentry.
  694. *
  695. * However, we can only permit this if it's a terminal point in
  696. * the path being looked up; if it wasn't then the remainder of
  697. * the path is inaccessible and we should say so.
  698. */
  699. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
  700. return -EREMOTE;
  701. return PTR_ERR(mnt);
  702. }
  703. if (!mnt) /* mount collision */
  704. return 0;
  705. if (!*need_mntput) {
  706. /* lock_mount() may release path->mnt on error */
  707. mntget(path->mnt);
  708. *need_mntput = true;
  709. }
  710. err = finish_automount(mnt, path);
  711. switch (err) {
  712. case -EBUSY:
  713. /* Someone else made a mount here whilst we were busy */
  714. return 0;
  715. case 0:
  716. path_put(path);
  717. path->mnt = mnt;
  718. path->dentry = dget(mnt->mnt_root);
  719. return 0;
  720. default:
  721. return err;
  722. }
  723. }
  724. /*
  725. * Handle a dentry that is managed in some way.
  726. * - Flagged for transit management (autofs)
  727. * - Flagged as mountpoint
  728. * - Flagged as automount point
  729. *
  730. * This may only be called in refwalk mode.
  731. *
  732. * Serialization is taken care of in namespace.c
  733. */
  734. static int follow_managed(struct path *path, unsigned flags)
  735. {
  736. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  737. unsigned managed;
  738. bool need_mntput = false;
  739. int ret = 0;
  740. /* Given that we're not holding a lock here, we retain the value in a
  741. * local variable for each dentry as we look at it so that we don't see
  742. * the components of that value change under us */
  743. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  744. managed &= DCACHE_MANAGED_DENTRY,
  745. unlikely(managed != 0)) {
  746. /* Allow the filesystem to manage the transit without i_mutex
  747. * being held. */
  748. if (managed & DCACHE_MANAGE_TRANSIT) {
  749. BUG_ON(!path->dentry->d_op);
  750. BUG_ON(!path->dentry->d_op->d_manage);
  751. ret = path->dentry->d_op->d_manage(path->dentry, false);
  752. if (ret < 0)
  753. break;
  754. }
  755. /* Transit to a mounted filesystem. */
  756. if (managed & DCACHE_MOUNTED) {
  757. struct vfsmount *mounted = lookup_mnt(path);
  758. if (mounted) {
  759. dput(path->dentry);
  760. if (need_mntput)
  761. mntput(path->mnt);
  762. path->mnt = mounted;
  763. path->dentry = dget(mounted->mnt_root);
  764. need_mntput = true;
  765. continue;
  766. }
  767. /* Something is mounted on this dentry in another
  768. * namespace and/or whatever was mounted there in this
  769. * namespace got unmounted before we managed to get the
  770. * vfsmount_lock */
  771. }
  772. /* Handle an automount point */
  773. if (managed & DCACHE_NEED_AUTOMOUNT) {
  774. ret = follow_automount(path, flags, &need_mntput);
  775. if (ret < 0)
  776. break;
  777. continue;
  778. }
  779. /* We didn't change the current path point */
  780. break;
  781. }
  782. if (need_mntput && path->mnt == mnt)
  783. mntput(path->mnt);
  784. if (ret == -EISDIR)
  785. ret = 0;
  786. return ret < 0 ? ret : need_mntput;
  787. }
  788. int follow_down_one(struct path *path)
  789. {
  790. struct vfsmount *mounted;
  791. mounted = lookup_mnt(path);
  792. if (mounted) {
  793. dput(path->dentry);
  794. mntput(path->mnt);
  795. path->mnt = mounted;
  796. path->dentry = dget(mounted->mnt_root);
  797. return 1;
  798. }
  799. return 0;
  800. }
  801. static inline bool managed_dentry_might_block(struct dentry *dentry)
  802. {
  803. return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
  804. dentry->d_op->d_manage(dentry, true) < 0);
  805. }
  806. /*
  807. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  808. * we meet a managed dentry that would need blocking.
  809. */
  810. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  811. struct inode **inode)
  812. {
  813. for (;;) {
  814. struct mount *mounted;
  815. /*
  816. * Don't forget we might have a non-mountpoint managed dentry
  817. * that wants to block transit.
  818. */
  819. if (unlikely(managed_dentry_might_block(path->dentry)))
  820. return false;
  821. if (!d_mountpoint(path->dentry))
  822. break;
  823. mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  824. if (!mounted)
  825. break;
  826. path->mnt = &mounted->mnt;
  827. path->dentry = mounted->mnt.mnt_root;
  828. nd->flags |= LOOKUP_JUMPED;
  829. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  830. /*
  831. * Update the inode too. We don't need to re-check the
  832. * dentry sequence number here after this d_inode read,
  833. * because a mount-point is always pinned.
  834. */
  835. *inode = path->dentry->d_inode;
  836. }
  837. return true;
  838. }
  839. static void follow_mount_rcu(struct nameidata *nd)
  840. {
  841. while (d_mountpoint(nd->path.dentry)) {
  842. struct mount *mounted;
  843. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
  844. if (!mounted)
  845. break;
  846. nd->path.mnt = &mounted->mnt;
  847. nd->path.dentry = mounted->mnt.mnt_root;
  848. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  849. }
  850. }
  851. static int follow_dotdot_rcu(struct nameidata *nd)
  852. {
  853. set_root_rcu(nd);
  854. while (1) {
  855. if (nd->path.dentry == nd->root.dentry &&
  856. nd->path.mnt == nd->root.mnt) {
  857. break;
  858. }
  859. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  860. struct dentry *old = nd->path.dentry;
  861. struct dentry *parent = old->d_parent;
  862. unsigned seq;
  863. seq = read_seqcount_begin(&parent->d_seq);
  864. if (read_seqcount_retry(&old->d_seq, nd->seq))
  865. goto failed;
  866. nd->path.dentry = parent;
  867. nd->seq = seq;
  868. break;
  869. }
  870. if (!follow_up_rcu(&nd->path))
  871. break;
  872. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  873. }
  874. follow_mount_rcu(nd);
  875. nd->inode = nd->path.dentry->d_inode;
  876. return 0;
  877. failed:
  878. nd->flags &= ~LOOKUP_RCU;
  879. if (!(nd->flags & LOOKUP_ROOT))
  880. nd->root.mnt = NULL;
  881. unlock_rcu_walk();
  882. return -ECHILD;
  883. }
  884. /*
  885. * Follow down to the covering mount currently visible to userspace. At each
  886. * point, the filesystem owning that dentry may be queried as to whether the
  887. * caller is permitted to proceed or not.
  888. */
  889. int follow_down(struct path *path)
  890. {
  891. unsigned managed;
  892. int ret;
  893. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  894. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  895. /* Allow the filesystem to manage the transit without i_mutex
  896. * being held.
  897. *
  898. * We indicate to the filesystem if someone is trying to mount
  899. * something here. This gives autofs the chance to deny anyone
  900. * other than its daemon the right to mount on its
  901. * superstructure.
  902. *
  903. * The filesystem may sleep at this point.
  904. */
  905. if (managed & DCACHE_MANAGE_TRANSIT) {
  906. BUG_ON(!path->dentry->d_op);
  907. BUG_ON(!path->dentry->d_op->d_manage);
  908. ret = path->dentry->d_op->d_manage(
  909. path->dentry, false);
  910. if (ret < 0)
  911. return ret == -EISDIR ? 0 : ret;
  912. }
  913. /* Transit to a mounted filesystem. */
  914. if (managed & DCACHE_MOUNTED) {
  915. struct vfsmount *mounted = lookup_mnt(path);
  916. if (!mounted)
  917. break;
  918. dput(path->dentry);
  919. mntput(path->mnt);
  920. path->mnt = mounted;
  921. path->dentry = dget(mounted->mnt_root);
  922. continue;
  923. }
  924. /* Don't handle automount points here */
  925. break;
  926. }
  927. return 0;
  928. }
  929. /*
  930. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  931. */
  932. static void follow_mount(struct path *path)
  933. {
  934. while (d_mountpoint(path->dentry)) {
  935. struct vfsmount *mounted = lookup_mnt(path);
  936. if (!mounted)
  937. break;
  938. dput(path->dentry);
  939. mntput(path->mnt);
  940. path->mnt = mounted;
  941. path->dentry = dget(mounted->mnt_root);
  942. }
  943. }
  944. static void follow_dotdot(struct nameidata *nd)
  945. {
  946. set_root(nd);
  947. while(1) {
  948. struct dentry *old = nd->path.dentry;
  949. if (nd->path.dentry == nd->root.dentry &&
  950. nd->path.mnt == nd->root.mnt) {
  951. break;
  952. }
  953. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  954. /* rare case of legitimate dget_parent()... */
  955. nd->path.dentry = dget_parent(nd->path.dentry);
  956. dput(old);
  957. break;
  958. }
  959. if (!follow_up(&nd->path))
  960. break;
  961. }
  962. follow_mount(&nd->path);
  963. nd->inode = nd->path.dentry->d_inode;
  964. }
  965. /*
  966. * This looks up the name in dcache, possibly revalidates the old dentry and
  967. * allocates a new one if not found or not valid. In the need_lookup argument
  968. * returns whether i_op->lookup is necessary.
  969. *
  970. * dir->d_inode->i_mutex must be held
  971. */
  972. static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
  973. unsigned int flags, bool *need_lookup)
  974. {
  975. struct dentry *dentry;
  976. int error;
  977. *need_lookup = false;
  978. dentry = d_lookup(dir, name);
  979. if (dentry) {
  980. if (d_need_lookup(dentry)) {
  981. *need_lookup = true;
  982. } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
  983. error = d_revalidate(dentry, flags);
  984. if (unlikely(error <= 0)) {
  985. if (error < 0) {
  986. dput(dentry);
  987. return ERR_PTR(error);
  988. } else if (!d_invalidate(dentry)) {
  989. dput(dentry);
  990. dentry = NULL;
  991. }
  992. }
  993. }
  994. }
  995. if (!dentry) {
  996. dentry = d_alloc(dir, name);
  997. if (unlikely(!dentry))
  998. return ERR_PTR(-ENOMEM);
  999. *need_lookup = true;
  1000. }
  1001. return dentry;
  1002. }
  1003. /*
  1004. * Call i_op->lookup on the dentry. The dentry must be negative but may be
  1005. * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
  1006. *
  1007. * dir->d_inode->i_mutex must be held
  1008. */
  1009. static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
  1010. unsigned int flags)
  1011. {
  1012. struct dentry *old;
  1013. /* Don't create child dentry for a dead directory. */
  1014. if (unlikely(IS_DEADDIR(dir))) {
  1015. dput(dentry);
  1016. return ERR_PTR(-ENOENT);
  1017. }
  1018. old = dir->i_op->lookup(dir, dentry, flags);
  1019. if (unlikely(old)) {
  1020. dput(dentry);
  1021. dentry = old;
  1022. }
  1023. return dentry;
  1024. }
  1025. static struct dentry *__lookup_hash(struct qstr *name,
  1026. struct dentry *base, unsigned int flags)
  1027. {
  1028. bool need_lookup;
  1029. struct dentry *dentry;
  1030. dentry = lookup_dcache(name, base, flags, &need_lookup);
  1031. if (!need_lookup)
  1032. return dentry;
  1033. return lookup_real(base->d_inode, dentry, flags);
  1034. }
  1035. /*
  1036. * It's more convoluted than I'd like it to be, but... it's still fairly
  1037. * small and for now I'd prefer to have fast path as straight as possible.
  1038. * It _is_ time-critical.
  1039. */
  1040. static int lookup_fast(struct nameidata *nd, struct qstr *name,
  1041. struct path *path, struct inode **inode)
  1042. {
  1043. struct vfsmount *mnt = nd->path.mnt;
  1044. struct dentry *dentry, *parent = nd->path.dentry;
  1045. int need_reval = 1;
  1046. int status = 1;
  1047. int err;
  1048. /*
  1049. * Rename seqlock is not required here because in the off chance
  1050. * of a false negative due to a concurrent rename, we're going to
  1051. * do the non-racy lookup, below.
  1052. */
  1053. if (nd->flags & LOOKUP_RCU) {
  1054. unsigned seq;
  1055. dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
  1056. if (!dentry)
  1057. goto unlazy;
  1058. /*
  1059. * This sequence count validates that the inode matches
  1060. * the dentry name information from lookup.
  1061. */
  1062. *inode = dentry->d_inode;
  1063. if (read_seqcount_retry(&dentry->d_seq, seq))
  1064. return -ECHILD;
  1065. /*
  1066. * This sequence count validates that the parent had no
  1067. * changes while we did the lookup of the dentry above.
  1068. *
  1069. * The memory barrier in read_seqcount_begin of child is
  1070. * enough, we can use __read_seqcount_retry here.
  1071. */
  1072. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1073. return -ECHILD;
  1074. nd->seq = seq;
  1075. if (unlikely(d_need_lookup(dentry)))
  1076. goto unlazy;
  1077. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1078. status = d_revalidate(dentry, nd->flags);
  1079. if (unlikely(status <= 0)) {
  1080. if (status != -ECHILD)
  1081. need_reval = 0;
  1082. goto unlazy;
  1083. }
  1084. }
  1085. path->mnt = mnt;
  1086. path->dentry = dentry;
  1087. if (unlikely(!__follow_mount_rcu(nd, path, inode)))
  1088. goto unlazy;
  1089. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  1090. goto unlazy;
  1091. return 0;
  1092. unlazy:
  1093. if (unlazy_walk(nd, dentry))
  1094. return -ECHILD;
  1095. } else {
  1096. dentry = __d_lookup(parent, name);
  1097. }
  1098. if (unlikely(!dentry))
  1099. goto need_lookup;
  1100. if (unlikely(d_need_lookup(dentry))) {
  1101. dput(dentry);
  1102. goto need_lookup;
  1103. }
  1104. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1105. status = d_revalidate(dentry, nd->flags);
  1106. if (unlikely(status <= 0)) {
  1107. if (status < 0) {
  1108. dput(dentry);
  1109. return status;
  1110. }
  1111. if (!d_invalidate(dentry)) {
  1112. dput(dentry);
  1113. goto need_lookup;
  1114. }
  1115. }
  1116. path->mnt = mnt;
  1117. path->dentry = dentry;
  1118. err = follow_managed(path, nd->flags);
  1119. if (unlikely(err < 0)) {
  1120. path_put_conditional(path, nd);
  1121. return err;
  1122. }
  1123. if (err)
  1124. nd->flags |= LOOKUP_JUMPED;
  1125. *inode = path->dentry->d_inode;
  1126. return 0;
  1127. need_lookup:
  1128. return 1;
  1129. }
  1130. /* Fast lookup failed, do it the slow way */
  1131. static int lookup_slow(struct nameidata *nd, struct qstr *name,
  1132. struct path *path)
  1133. {
  1134. struct dentry *dentry, *parent;
  1135. int err;
  1136. parent = nd->path.dentry;
  1137. BUG_ON(nd->inode != parent->d_inode);
  1138. mutex_lock(&parent->d_inode->i_mutex);
  1139. dentry = __lookup_hash(name, parent, nd->flags);
  1140. mutex_unlock(&parent->d_inode->i_mutex);
  1141. if (IS_ERR(dentry))
  1142. return PTR_ERR(dentry);
  1143. path->mnt = nd->path.mnt;
  1144. path->dentry = dentry;
  1145. err = follow_managed(path, nd->flags);
  1146. if (unlikely(err < 0)) {
  1147. path_put_conditional(path, nd);
  1148. return err;
  1149. }
  1150. if (err)
  1151. nd->flags |= LOOKUP_JUMPED;
  1152. return 0;
  1153. }
  1154. static inline int may_lookup(struct nameidata *nd)
  1155. {
  1156. if (nd->flags & LOOKUP_RCU) {
  1157. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1158. if (err != -ECHILD)
  1159. return err;
  1160. if (unlazy_walk(nd, NULL))
  1161. return -ECHILD;
  1162. }
  1163. return inode_permission(nd->inode, MAY_EXEC);
  1164. }
  1165. static inline int handle_dots(struct nameidata *nd, int type)
  1166. {
  1167. if (type == LAST_DOTDOT) {
  1168. if (nd->flags & LOOKUP_RCU) {
  1169. if (follow_dotdot_rcu(nd))
  1170. return -ECHILD;
  1171. } else
  1172. follow_dotdot(nd);
  1173. }
  1174. return 0;
  1175. }
  1176. static void terminate_walk(struct nameidata *nd)
  1177. {
  1178. if (!(nd->flags & LOOKUP_RCU)) {
  1179. path_put(&nd->path);
  1180. } else {
  1181. nd->flags &= ~LOOKUP_RCU;
  1182. if (!(nd->flags & LOOKUP_ROOT))
  1183. nd->root.mnt = NULL;
  1184. unlock_rcu_walk();
  1185. }
  1186. }
  1187. /*
  1188. * Do we need to follow links? We _really_ want to be able
  1189. * to do this check without having to look at inode->i_op,
  1190. * so we keep a cache of "no, this doesn't need follow_link"
  1191. * for the common case.
  1192. */
  1193. static inline int should_follow_link(struct inode *inode, int follow)
  1194. {
  1195. if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
  1196. if (likely(inode->i_op->follow_link))
  1197. return follow;
  1198. /* This gets set once for the inode lifetime */
  1199. spin_lock(&inode->i_lock);
  1200. inode->i_opflags |= IOP_NOFOLLOW;
  1201. spin_unlock(&inode->i_lock);
  1202. }
  1203. return 0;
  1204. }
  1205. static inline int walk_component(struct nameidata *nd, struct path *path,
  1206. struct qstr *name, int type, int follow)
  1207. {
  1208. struct inode *inode;
  1209. int err;
  1210. /*
  1211. * "." and ".." are special - ".." especially so because it has
  1212. * to be able to know about the current root directory and
  1213. * parent relationships.
  1214. */
  1215. if (unlikely(type != LAST_NORM))
  1216. return handle_dots(nd, type);
  1217. err = lookup_fast(nd, name, path, &inode);
  1218. if (unlikely(err)) {
  1219. if (err < 0)
  1220. goto out_err;
  1221. err = lookup_slow(nd, name, path);
  1222. if (err < 0)
  1223. goto out_err;
  1224. inode = path->dentry->d_inode;
  1225. }
  1226. err = -ENOENT;
  1227. if (!inode)
  1228. goto out_path_put;
  1229. if (should_follow_link(inode, follow)) {
  1230. if (nd->flags & LOOKUP_RCU) {
  1231. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1232. err = -ECHILD;
  1233. goto out_err;
  1234. }
  1235. }
  1236. BUG_ON(inode != path->dentry->d_inode);
  1237. return 1;
  1238. }
  1239. path_to_nameidata(path, nd);
  1240. nd->inode = inode;
  1241. return 0;
  1242. out_path_put:
  1243. path_to_nameidata(path, nd);
  1244. out_err:
  1245. terminate_walk(nd);
  1246. return err;
  1247. }
  1248. /*
  1249. * This limits recursive symlink follows to 8, while
  1250. * limiting consecutive symlinks to 40.
  1251. *
  1252. * Without that kind of total limit, nasty chains of consecutive
  1253. * symlinks can cause almost arbitrarily long lookups.
  1254. */
  1255. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1256. {
  1257. int res;
  1258. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1259. path_put_conditional(path, nd);
  1260. path_put(&nd->path);
  1261. return -ELOOP;
  1262. }
  1263. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1264. nd->depth++;
  1265. current->link_count++;
  1266. do {
  1267. struct path link = *path;
  1268. void *cookie;
  1269. res = follow_link(&link, nd, &cookie);
  1270. if (res)
  1271. break;
  1272. res = walk_component(nd, path, &nd->last,
  1273. nd->last_type, LOOKUP_FOLLOW);
  1274. put_link(nd, &link, cookie);
  1275. } while (res > 0);
  1276. current->link_count--;
  1277. nd->depth--;
  1278. return res;
  1279. }
  1280. /*
  1281. * We really don't want to look at inode->i_op->lookup
  1282. * when we don't have to. So we keep a cache bit in
  1283. * the inode ->i_opflags field that says "yes, we can
  1284. * do lookup on this inode".
  1285. */
  1286. static inline int can_lookup(struct inode *inode)
  1287. {
  1288. if (likely(inode->i_opflags & IOP_LOOKUP))
  1289. return 1;
  1290. if (likely(!inode->i_op->lookup))
  1291. return 0;
  1292. /* We do this once for the lifetime of the inode */
  1293. spin_lock(&inode->i_lock);
  1294. inode->i_opflags |= IOP_LOOKUP;
  1295. spin_unlock(&inode->i_lock);
  1296. return 1;
  1297. }
  1298. /*
  1299. * We can do the critical dentry name comparison and hashing
  1300. * operations one word at a time, but we are limited to:
  1301. *
  1302. * - Architectures with fast unaligned word accesses. We could
  1303. * do a "get_unaligned()" if this helps and is sufficiently
  1304. * fast.
  1305. *
  1306. * - Little-endian machines (so that we can generate the mask
  1307. * of low bytes efficiently). Again, we *could* do a byte
  1308. * swapping load on big-endian architectures if that is not
  1309. * expensive enough to make the optimization worthless.
  1310. *
  1311. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1312. * do not trap on the (extremely unlikely) case of a page
  1313. * crossing operation.
  1314. *
  1315. * - Furthermore, we need an efficient 64-bit compile for the
  1316. * 64-bit case in order to generate the "number of bytes in
  1317. * the final mask". Again, that could be replaced with a
  1318. * efficient population count instruction or similar.
  1319. */
  1320. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1321. #include <asm/word-at-a-time.h>
  1322. #ifdef CONFIG_64BIT
  1323. static inline unsigned int fold_hash(unsigned long hash)
  1324. {
  1325. hash += hash >> (8*sizeof(int));
  1326. return hash;
  1327. }
  1328. #else /* 32-bit case */
  1329. #define fold_hash(x) (x)
  1330. #endif
  1331. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1332. {
  1333. unsigned long a, mask;
  1334. unsigned long hash = 0;
  1335. for (;;) {
  1336. a = load_unaligned_zeropad(name);
  1337. if (len < sizeof(unsigned long))
  1338. break;
  1339. hash += a;
  1340. hash *= 9;
  1341. name += sizeof(unsigned long);
  1342. len -= sizeof(unsigned long);
  1343. if (!len)
  1344. goto done;
  1345. }
  1346. mask = ~(~0ul << len*8);
  1347. hash += mask & a;
  1348. done:
  1349. return fold_hash(hash);
  1350. }
  1351. EXPORT_SYMBOL(full_name_hash);
  1352. /*
  1353. * Calculate the length and hash of the path component, and
  1354. * return the length of the component;
  1355. */
  1356. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1357. {
  1358. unsigned long a, b, adata, bdata, mask, hash, len;
  1359. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1360. hash = a = 0;
  1361. len = -sizeof(unsigned long);
  1362. do {
  1363. hash = (hash + a) * 9;
  1364. len += sizeof(unsigned long);
  1365. a = load_unaligned_zeropad(name+len);
  1366. b = a ^ REPEAT_BYTE('/');
  1367. } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
  1368. adata = prep_zero_mask(a, adata, &constants);
  1369. bdata = prep_zero_mask(b, bdata, &constants);
  1370. mask = create_zero_mask(adata | bdata);
  1371. hash += a & zero_bytemask(mask);
  1372. *hashp = fold_hash(hash);
  1373. return len + find_zero(mask);
  1374. }
  1375. #else
  1376. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1377. {
  1378. unsigned long hash = init_name_hash();
  1379. while (len--)
  1380. hash = partial_name_hash(*name++, hash);
  1381. return end_name_hash(hash);
  1382. }
  1383. EXPORT_SYMBOL(full_name_hash);
  1384. /*
  1385. * We know there's a real path component here of at least
  1386. * one character.
  1387. */
  1388. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1389. {
  1390. unsigned long hash = init_name_hash();
  1391. unsigned long len = 0, c;
  1392. c = (unsigned char)*name;
  1393. do {
  1394. len++;
  1395. hash = partial_name_hash(c, hash);
  1396. c = (unsigned char)name[len];
  1397. } while (c && c != '/');
  1398. *hashp = end_name_hash(hash);
  1399. return len;
  1400. }
  1401. #endif
  1402. /*
  1403. * Name resolution.
  1404. * This is the basic name resolution function, turning a pathname into
  1405. * the final dentry. We expect 'base' to be positive and a directory.
  1406. *
  1407. * Returns 0 and nd will have valid dentry and mnt on success.
  1408. * Returns error and drops reference to input namei data on failure.
  1409. */
  1410. static int link_path_walk(const char *name, struct nameidata *nd)
  1411. {
  1412. struct path next;
  1413. int err;
  1414. while (*name=='/')
  1415. name++;
  1416. if (!*name)
  1417. return 0;
  1418. /* At this point we know we have a real path component. */
  1419. for(;;) {
  1420. struct qstr this;
  1421. long len;
  1422. int type;
  1423. err = may_lookup(nd);
  1424. if (err)
  1425. break;
  1426. len = hash_name(name, &this.hash);
  1427. this.name = name;
  1428. this.len = len;
  1429. type = LAST_NORM;
  1430. if (name[0] == '.') switch (len) {
  1431. case 2:
  1432. if (name[1] == '.') {
  1433. type = LAST_DOTDOT;
  1434. nd->flags |= LOOKUP_JUMPED;
  1435. }
  1436. break;
  1437. case 1:
  1438. type = LAST_DOT;
  1439. }
  1440. if (likely(type == LAST_NORM)) {
  1441. struct dentry *parent = nd->path.dentry;
  1442. nd->flags &= ~LOOKUP_JUMPED;
  1443. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1444. err = parent->d_op->d_hash(parent, nd->inode,
  1445. &this);
  1446. if (err < 0)
  1447. break;
  1448. }
  1449. }
  1450. if (!name[len])
  1451. goto last_component;
  1452. /*
  1453. * If it wasn't NUL, we know it was '/'. Skip that
  1454. * slash, and continue until no more slashes.
  1455. */
  1456. do {
  1457. len++;
  1458. } while (unlikely(name[len] == '/'));
  1459. if (!name[len])
  1460. goto last_component;
  1461. name += len;
  1462. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1463. if (err < 0)
  1464. return err;
  1465. if (err) {
  1466. err = nested_symlink(&next, nd);
  1467. if (err)
  1468. return err;
  1469. }
  1470. if (can_lookup(nd->inode))
  1471. continue;
  1472. err = -ENOTDIR;
  1473. break;
  1474. /* here ends the main loop */
  1475. last_component:
  1476. nd->last = this;
  1477. nd->last_type = type;
  1478. return 0;
  1479. }
  1480. terminate_walk(nd);
  1481. return err;
  1482. }
  1483. static int path_init(int dfd, const char *name, unsigned int flags,
  1484. struct nameidata *nd, struct file **fp)
  1485. {
  1486. int retval = 0;
  1487. int fput_needed;
  1488. struct file *file;
  1489. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1490. nd->flags = flags | LOOKUP_JUMPED;
  1491. nd->depth = 0;
  1492. if (flags & LOOKUP_ROOT) {
  1493. struct inode *inode = nd->root.dentry->d_inode;
  1494. if (*name) {
  1495. if (!inode->i_op->lookup)
  1496. return -ENOTDIR;
  1497. retval = inode_permission(inode, MAY_EXEC);
  1498. if (retval)
  1499. return retval;
  1500. }
  1501. nd->path = nd->root;
  1502. nd->inode = inode;
  1503. if (flags & LOOKUP_RCU) {
  1504. lock_rcu_walk();
  1505. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1506. } else {
  1507. path_get(&nd->path);
  1508. }
  1509. return 0;
  1510. }
  1511. nd->root.mnt = NULL;
  1512. if (*name=='/') {
  1513. if (flags & LOOKUP_RCU) {
  1514. lock_rcu_walk();
  1515. set_root_rcu(nd);
  1516. } else {
  1517. set_root(nd);
  1518. path_get(&nd->root);
  1519. }
  1520. nd->path = nd->root;
  1521. } else if (dfd == AT_FDCWD) {
  1522. if (flags & LOOKUP_RCU) {
  1523. struct fs_struct *fs = current->fs;
  1524. unsigned seq;
  1525. lock_rcu_walk();
  1526. do {
  1527. seq = read_seqcount_begin(&fs->seq);
  1528. nd->path = fs->pwd;
  1529. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1530. } while (read_seqcount_retry(&fs->seq, seq));
  1531. } else {
  1532. get_fs_pwd(current->fs, &nd->path);
  1533. }
  1534. } else {
  1535. struct dentry *dentry;
  1536. file = fget_raw_light(dfd, &fput_needed);
  1537. retval = -EBADF;
  1538. if (!file)
  1539. goto out_fail;
  1540. dentry = file->f_path.dentry;
  1541. if (*name) {
  1542. retval = -ENOTDIR;
  1543. if (!S_ISDIR(dentry->d_inode->i_mode))
  1544. goto fput_fail;
  1545. retval = inode_permission(dentry->d_inode, MAY_EXEC);
  1546. if (retval)
  1547. goto fput_fail;
  1548. }
  1549. nd->path = file->f_path;
  1550. if (flags & LOOKUP_RCU) {
  1551. if (fput_needed)
  1552. *fp = file;
  1553. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1554. lock_rcu_walk();
  1555. } else {
  1556. path_get(&file->f_path);
  1557. fput_light(file, fput_needed);
  1558. }
  1559. }
  1560. nd->inode = nd->path.dentry->d_inode;
  1561. return 0;
  1562. fput_fail:
  1563. fput_light(file, fput_needed);
  1564. out_fail:
  1565. return retval;
  1566. }
  1567. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1568. {
  1569. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1570. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1571. nd->flags &= ~LOOKUP_PARENT;
  1572. return walk_component(nd, path, &nd->last, nd->last_type,
  1573. nd->flags & LOOKUP_FOLLOW);
  1574. }
  1575. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1576. static int path_lookupat(int dfd, const char *name,
  1577. unsigned int flags, struct nameidata *nd)
  1578. {
  1579. struct file *base = NULL;
  1580. struct path path;
  1581. int err;
  1582. /*
  1583. * Path walking is largely split up into 2 different synchronisation
  1584. * schemes, rcu-walk and ref-walk (explained in
  1585. * Documentation/filesystems/path-lookup.txt). These share much of the
  1586. * path walk code, but some things particularly setup, cleanup, and
  1587. * following mounts are sufficiently divergent that functions are
  1588. * duplicated. Typically there is a function foo(), and its RCU
  1589. * analogue, foo_rcu().
  1590. *
  1591. * -ECHILD is the error number of choice (just to avoid clashes) that
  1592. * is returned if some aspect of an rcu-walk fails. Such an error must
  1593. * be handled by restarting a traditional ref-walk (which will always
  1594. * be able to complete).
  1595. */
  1596. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1597. if (unlikely(err))
  1598. return err;
  1599. current->total_link_count = 0;
  1600. err = link_path_walk(name, nd);
  1601. if (!err && !(flags & LOOKUP_PARENT)) {
  1602. err = lookup_last(nd, &path);
  1603. while (err > 0) {
  1604. void *cookie;
  1605. struct path link = path;
  1606. nd->flags |= LOOKUP_PARENT;
  1607. err = follow_link(&link, nd, &cookie);
  1608. if (err)
  1609. break;
  1610. err = lookup_last(nd, &path);
  1611. put_link(nd, &link, cookie);
  1612. }
  1613. }
  1614. if (!err)
  1615. err = complete_walk(nd);
  1616. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1617. if (!nd->inode->i_op->lookup) {
  1618. path_put(&nd->path);
  1619. err = -ENOTDIR;
  1620. }
  1621. }
  1622. if (base)
  1623. fput(base);
  1624. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1625. path_put(&nd->root);
  1626. nd->root.mnt = NULL;
  1627. }
  1628. return err;
  1629. }
  1630. static int do_path_lookup(int dfd, const char *name,
  1631. unsigned int flags, struct nameidata *nd)
  1632. {
  1633. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1634. if (unlikely(retval == -ECHILD))
  1635. retval = path_lookupat(dfd, name, flags, nd);
  1636. if (unlikely(retval == -ESTALE))
  1637. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1638. if (likely(!retval)) {
  1639. if (unlikely(!audit_dummy_context())) {
  1640. if (nd->path.dentry && nd->inode)
  1641. audit_inode(name, nd->path.dentry);
  1642. }
  1643. }
  1644. return retval;
  1645. }
  1646. /* does lookup, returns the object with parent locked */
  1647. struct dentry *kern_path_locked(const char *name, struct path *path)
  1648. {
  1649. struct nameidata nd;
  1650. struct dentry *d;
  1651. int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
  1652. if (err)
  1653. return ERR_PTR(err);
  1654. if (nd.last_type != LAST_NORM) {
  1655. path_put(&nd.path);
  1656. return ERR_PTR(-EINVAL);
  1657. }
  1658. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1659. d = __lookup_hash(&nd.last, nd.path.dentry, 0);
  1660. if (IS_ERR(d)) {
  1661. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1662. path_put(&nd.path);
  1663. return d;
  1664. }
  1665. *path = nd.path;
  1666. return d;
  1667. }
  1668. int kern_path(const char *name, unsigned int flags, struct path *path)
  1669. {
  1670. struct nameidata nd;
  1671. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1672. if (!res)
  1673. *path = nd.path;
  1674. return res;
  1675. }
  1676. /**
  1677. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1678. * @dentry: pointer to dentry of the base directory
  1679. * @mnt: pointer to vfs mount of the base directory
  1680. * @name: pointer to file name
  1681. * @flags: lookup flags
  1682. * @path: pointer to struct path to fill
  1683. */
  1684. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1685. const char *name, unsigned int flags,
  1686. struct path *path)
  1687. {
  1688. struct nameidata nd;
  1689. int err;
  1690. nd.root.dentry = dentry;
  1691. nd.root.mnt = mnt;
  1692. BUG_ON(flags & LOOKUP_PARENT);
  1693. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1694. err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  1695. if (!err)
  1696. *path = nd.path;
  1697. return err;
  1698. }
  1699. /*
  1700. * Restricted form of lookup. Doesn't follow links, single-component only,
  1701. * needs parent already locked. Doesn't follow mounts.
  1702. * SMP-safe.
  1703. */
  1704. static struct dentry *lookup_hash(struct nameidata *nd)
  1705. {
  1706. return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
  1707. }
  1708. /**
  1709. * lookup_one_len - filesystem helper to lookup single pathname component
  1710. * @name: pathname component to lookup
  1711. * @base: base directory to lookup from
  1712. * @len: maximum length @len should be interpreted to
  1713. *
  1714. * Note that this routine is purely a helper for filesystem usage and should
  1715. * not be called by generic code. Also note that by using this function the
  1716. * nameidata argument is passed to the filesystem methods and a filesystem
  1717. * using this helper needs to be prepared for that.
  1718. */
  1719. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1720. {
  1721. struct qstr this;
  1722. unsigned int c;
  1723. int err;
  1724. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1725. this.name = name;
  1726. this.len = len;
  1727. this.hash = full_name_hash(name, len);
  1728. if (!len)
  1729. return ERR_PTR(-EACCES);
  1730. while (len--) {
  1731. c = *(const unsigned char *)name++;
  1732. if (c == '/' || c == '\0')
  1733. return ERR_PTR(-EACCES);
  1734. }
  1735. /*
  1736. * See if the low-level filesystem might want
  1737. * to use its own hash..
  1738. */
  1739. if (base->d_flags & DCACHE_OP_HASH) {
  1740. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1741. if (err < 0)
  1742. return ERR_PTR(err);
  1743. }
  1744. err = inode_permission(base->d_inode, MAY_EXEC);
  1745. if (err)
  1746. return ERR_PTR(err);
  1747. return __lookup_hash(&this, base, 0);
  1748. }
  1749. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  1750. struct path *path, int *empty)
  1751. {
  1752. struct nameidata nd;
  1753. char *tmp = getname_flags(name, flags, empty);
  1754. int err = PTR_ERR(tmp);
  1755. if (!IS_ERR(tmp)) {
  1756. BUG_ON(flags & LOOKUP_PARENT);
  1757. err = do_path_lookup(dfd, tmp, flags, &nd);
  1758. putname(tmp);
  1759. if (!err)
  1760. *path = nd.path;
  1761. }
  1762. return err;
  1763. }
  1764. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1765. struct path *path)
  1766. {
  1767. return user_path_at_empty(dfd, name, flags, path, NULL);
  1768. }
  1769. static int user_path_parent(int dfd, const char __user *path,
  1770. struct nameidata *nd, char **name)
  1771. {
  1772. char *s = getname(path);
  1773. int error;
  1774. if (IS_ERR(s))
  1775. return PTR_ERR(s);
  1776. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1777. if (error)
  1778. putname(s);
  1779. else
  1780. *name = s;
  1781. return error;
  1782. }
  1783. /*
  1784. * It's inline, so penalty for filesystems that don't use sticky bit is
  1785. * minimal.
  1786. */
  1787. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1788. {
  1789. kuid_t fsuid = current_fsuid();
  1790. if (!(dir->i_mode & S_ISVTX))
  1791. return 0;
  1792. if (uid_eq(inode->i_uid, fsuid))
  1793. return 0;
  1794. if (uid_eq(dir->i_uid, fsuid))
  1795. return 0;
  1796. return !inode_capable(inode, CAP_FOWNER);
  1797. }
  1798. /*
  1799. * Check whether we can remove a link victim from directory dir, check
  1800. * whether the type of victim is right.
  1801. * 1. We can't do it if dir is read-only (done in permission())
  1802. * 2. We should have write and exec permissions on dir
  1803. * 3. We can't remove anything from append-only dir
  1804. * 4. We can't do anything with immutable dir (done in permission())
  1805. * 5. If the sticky bit on dir is set we should either
  1806. * a. be owner of dir, or
  1807. * b. be owner of victim, or
  1808. * c. have CAP_FOWNER capability
  1809. * 6. If the victim is append-only or immutable we can't do antyhing with
  1810. * links pointing to it.
  1811. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1812. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1813. * 9. We can't remove a root or mountpoint.
  1814. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1815. * nfs_async_unlink().
  1816. */
  1817. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1818. {
  1819. int error;
  1820. if (!victim->d_inode)
  1821. return -ENOENT;
  1822. BUG_ON(victim->d_parent->d_inode != dir);
  1823. audit_inode_child(victim, dir);
  1824. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1825. if (error)
  1826. return error;
  1827. if (IS_APPEND(dir))
  1828. return -EPERM;
  1829. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1830. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1831. return -EPERM;
  1832. if (isdir) {
  1833. if (!S_ISDIR(victim->d_inode->i_mode))
  1834. return -ENOTDIR;
  1835. if (IS_ROOT(victim))
  1836. return -EBUSY;
  1837. } else if (S_ISDIR(victim->d_inode->i_mode))
  1838. return -EISDIR;
  1839. if (IS_DEADDIR(dir))
  1840. return -ENOENT;
  1841. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1842. return -EBUSY;
  1843. return 0;
  1844. }
  1845. /* Check whether we can create an object with dentry child in directory
  1846. * dir.
  1847. * 1. We can't do it if child already exists (open has special treatment for
  1848. * this case, but since we are inlined it's OK)
  1849. * 2. We can't do it if dir is read-only (done in permission())
  1850. * 3. We should have write and exec permissions on dir
  1851. * 4. We can't do it if dir is immutable (done in permission())
  1852. */
  1853. static inline int may_create(struct inode *dir, struct dentry *child)
  1854. {
  1855. if (child->d_inode)
  1856. return -EEXIST;
  1857. if (IS_DEADDIR(dir))
  1858. return -ENOENT;
  1859. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1860. }
  1861. /*
  1862. * p1 and p2 should be directories on the same fs.
  1863. */
  1864. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1865. {
  1866. struct dentry *p;
  1867. if (p1 == p2) {
  1868. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1869. return NULL;
  1870. }
  1871. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1872. p = d_ancestor(p2, p1);
  1873. if (p) {
  1874. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1875. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1876. return p;
  1877. }
  1878. p = d_ancestor(p1, p2);
  1879. if (p) {
  1880. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1881. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1882. return p;
  1883. }
  1884. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1885. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1886. return NULL;
  1887. }
  1888. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1889. {
  1890. mutex_unlock(&p1->d_inode->i_mutex);
  1891. if (p1 != p2) {
  1892. mutex_unlock(&p2->d_inode->i_mutex);
  1893. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1894. }
  1895. }
  1896. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  1897. bool want_excl)
  1898. {
  1899. int error = may_create(dir, dentry);
  1900. if (error)
  1901. return error;
  1902. if (!dir->i_op->create)
  1903. return -EACCES; /* shouldn't it be ENOSYS? */
  1904. mode &= S_IALLUGO;
  1905. mode |= S_IFREG;
  1906. error = security_inode_create(dir, dentry, mode);
  1907. if (error)
  1908. return error;
  1909. error = dir->i_op->create(dir, dentry, mode, want_excl);
  1910. if (!error)
  1911. fsnotify_create(dir, dentry);
  1912. return error;
  1913. }
  1914. static int may_open(struct path *path, int acc_mode, int flag)
  1915. {
  1916. struct dentry *dentry = path->dentry;
  1917. struct inode *inode = dentry->d_inode;
  1918. int error;
  1919. /* O_PATH? */
  1920. if (!acc_mode)
  1921. return 0;
  1922. if (!inode)
  1923. return -ENOENT;
  1924. switch (inode->i_mode & S_IFMT) {
  1925. case S_IFLNK:
  1926. return -ELOOP;
  1927. case S_IFDIR:
  1928. if (acc_mode & MAY_WRITE)
  1929. return -EISDIR;
  1930. break;
  1931. case S_IFBLK:
  1932. case S_IFCHR:
  1933. if (path->mnt->mnt_flags & MNT_NODEV)
  1934. return -EACCES;
  1935. /*FALLTHRU*/
  1936. case S_IFIFO:
  1937. case S_IFSOCK:
  1938. flag &= ~O_TRUNC;
  1939. break;
  1940. }
  1941. error = inode_permission(inode, acc_mode);
  1942. if (error)
  1943. return error;
  1944. /*
  1945. * An append-only file must be opened in append mode for writing.
  1946. */
  1947. if (IS_APPEND(inode)) {
  1948. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1949. return -EPERM;
  1950. if (flag & O_TRUNC)
  1951. return -EPERM;
  1952. }
  1953. /* O_NOATIME can only be set by the owner or superuser */
  1954. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  1955. return -EPERM;
  1956. return 0;
  1957. }
  1958. static int handle_truncate(struct file *filp)
  1959. {
  1960. struct path *path = &filp->f_path;
  1961. struct inode *inode = path->dentry->d_inode;
  1962. int error = get_write_access(inode);
  1963. if (error)
  1964. return error;
  1965. /*
  1966. * Refuse to truncate files with mandatory locks held on them.
  1967. */
  1968. error = locks_verify_locked(inode);
  1969. if (!error)
  1970. error = security_path_truncate(path);
  1971. if (!error) {
  1972. error = do_truncate(path->dentry, 0,
  1973. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1974. filp);
  1975. }
  1976. put_write_access(inode);
  1977. return error;
  1978. }
  1979. static inline int open_to_namei_flags(int flag)
  1980. {
  1981. if ((flag & O_ACCMODE) == 3)
  1982. flag--;
  1983. return flag;
  1984. }
  1985. static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
  1986. {
  1987. int error = security_path_mknod(dir, dentry, mode, 0);
  1988. if (error)
  1989. return error;
  1990. error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
  1991. if (error)
  1992. return error;
  1993. return security_inode_create(dir->dentry->d_inode, dentry, mode);
  1994. }
  1995. /*
  1996. * Attempt to atomically look up, create and open a file from a negative
  1997. * dentry.
  1998. *
  1999. * Returns 0 if successful. The file will have been created and attached to
  2000. * @file by the filesystem calling finish_open().
  2001. *
  2002. * Returns 1 if the file was looked up only or didn't need creating. The
  2003. * caller will need to perform the open themselves. @path will have been
  2004. * updated to point to the new dentry. This may be negative.
  2005. *
  2006. * Returns an error code otherwise.
  2007. */
  2008. static int atomic_open(struct nameidata *nd, struct dentry *dentry,
  2009. struct path *path, struct file *file,
  2010. const struct open_flags *op,
  2011. bool *want_write, bool need_lookup,
  2012. int *opened)
  2013. {
  2014. struct inode *dir = nd->path.dentry->d_inode;
  2015. unsigned open_flag = open_to_namei_flags(op->open_flag);
  2016. umode_t mode;
  2017. int error;
  2018. int acc_mode;
  2019. int create_error = 0;
  2020. struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
  2021. BUG_ON(dentry->d_inode);
  2022. /* Don't create child dentry for a dead directory. */
  2023. if (unlikely(IS_DEADDIR(dir))) {
  2024. error = -ENOENT;
  2025. goto out;
  2026. }
  2027. mode = op->mode & S_IALLUGO;
  2028. if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
  2029. mode &= ~current_umask();
  2030. if (open_flag & O_EXCL) {
  2031. open_flag &= ~O_TRUNC;
  2032. *opened |= FILE_CREATED;
  2033. }
  2034. /*
  2035. * Checking write permission is tricky, bacuse we don't know if we are
  2036. * going to actually need it: O_CREAT opens should work as long as the
  2037. * file exists. But checking existence breaks atomicity. The trick is
  2038. * to check access and if not granted clear O_CREAT from the flags.
  2039. *
  2040. * Another problem is returing the "right" error value (e.g. for an
  2041. * O_EXCL open we want to return EEXIST not EROFS).
  2042. */
  2043. if ((open_flag & (O_CREAT | O_TRUNC)) ||
  2044. (open_flag & O_ACCMODE) != O_RDONLY) {
  2045. error = mnt_want_write(nd->path.mnt);
  2046. if (!error) {
  2047. *want_write = true;
  2048. } else if (!(open_flag & O_CREAT)) {
  2049. /*
  2050. * No O_CREATE -> atomicity not a requirement -> fall
  2051. * back to lookup + open
  2052. */
  2053. goto no_open;
  2054. } else if (open_flag & (O_EXCL | O_TRUNC)) {
  2055. /* Fall back and fail with the right error */
  2056. create_error = error;
  2057. goto no_open;
  2058. } else {
  2059. /* No side effects, safe to clear O_CREAT */
  2060. create_error = error;
  2061. open_flag &= ~O_CREAT;
  2062. }
  2063. }
  2064. if (open_flag & O_CREAT) {
  2065. error = may_o_create(&nd->path, dentry, op->mode);
  2066. if (error) {
  2067. create_error = error;
  2068. if (open_flag & O_EXCL)
  2069. goto no_open;
  2070. open_flag &= ~O_CREAT;
  2071. }
  2072. }
  2073. if (nd->flags & LOOKUP_DIRECTORY)
  2074. open_flag |= O_DIRECTORY;
  2075. file->f_path.dentry = DENTRY_NOT_SET;
  2076. file->f_path.mnt = nd->path.mnt;
  2077. error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
  2078. opened);
  2079. if (error < 0) {
  2080. if (create_error && error == -ENOENT)
  2081. error = create_error;
  2082. goto out;
  2083. }
  2084. acc_mode = op->acc_mode;
  2085. if (*opened & FILE_CREATED) {
  2086. fsnotify_create(dir, dentry);
  2087. acc_mode = MAY_OPEN;
  2088. }
  2089. if (error) { /* returned 1, that is */
  2090. if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
  2091. error = -EIO;
  2092. goto out;
  2093. }
  2094. if (file->f_path.dentry) {
  2095. dput(dentry);
  2096. dentry = file->f_path.dentry;
  2097. }
  2098. goto looked_up;
  2099. }
  2100. /*
  2101. * We didn't have the inode before the open, so check open permission
  2102. * here.
  2103. */
  2104. error = may_open(&file->f_path, acc_mode, open_flag);
  2105. if (error)
  2106. fput(file);
  2107. out:
  2108. dput(dentry);
  2109. return error;
  2110. no_open:
  2111. if (need_lookup) {
  2112. dentry = lookup_real(dir, dentry, nd->flags);
  2113. if (IS_ERR(dentry))
  2114. return PTR_ERR(dentry);
  2115. if (create_error) {
  2116. int open_flag = op->open_flag;
  2117. error = create_error;
  2118. if ((open_flag & O_EXCL)) {
  2119. if (!dentry->d_inode)
  2120. goto out;
  2121. } else if (!dentry->d_inode) {
  2122. goto out;
  2123. } else if ((open_flag & O_TRUNC) &&
  2124. S_ISREG(dentry->d_inode->i_mode)) {
  2125. goto out;
  2126. }
  2127. /* will fail later, go on to get the right error */
  2128. }
  2129. }
  2130. looked_up:
  2131. path->dentry = dentry;
  2132. path->mnt = nd->path.mnt;
  2133. return 1;
  2134. }
  2135. /*
  2136. * Look up and maybe create and open the last component.
  2137. *
  2138. * Must be called with i_mutex held on parent.
  2139. *
  2140. * Returns 0 if the file was successfully atomically created (if necessary) and
  2141. * opened. In this case the file will be returned attached to @file.
  2142. *
  2143. * Returns 1 if the file was not completely opened at this time, though lookups
  2144. * and creations will have been performed and the dentry returned in @path will
  2145. * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
  2146. * specified then a negative dentry may be returned.
  2147. *
  2148. * An error code is returned otherwise.
  2149. *
  2150. * FILE_CREATE will be set in @*opened if the dentry was created and will be
  2151. * cleared otherwise prior to returning.
  2152. */
  2153. static int lookup_open(struct nameidata *nd, struct path *path,
  2154. struct file *file,
  2155. const struct open_flags *op,
  2156. bool *want_write, int *opened)
  2157. {
  2158. struct dentry *dir = nd->path.dentry;
  2159. struct inode *dir_inode = dir->d_inode;
  2160. struct dentry *dentry;
  2161. int error;
  2162. bool need_lookup;
  2163. *opened &= ~FILE_CREATED;
  2164. dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
  2165. if (IS_ERR(dentry))
  2166. return PTR_ERR(dentry);
  2167. /* Cached positive dentry: will open in f_op->open */
  2168. if (!need_lookup && dentry->d_inode)
  2169. goto out_no_open;
  2170. if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
  2171. return atomic_open(nd, dentry, path, file, op, want_write,
  2172. need_lookup, opened);
  2173. }
  2174. if (need_lookup) {
  2175. BUG_ON(dentry->d_inode);
  2176. dentry = lookup_real(dir_inode, dentry, nd->flags);
  2177. if (IS_ERR(dentry))
  2178. return PTR_ERR(dentry);
  2179. }
  2180. /* Negative dentry, just create the file */
  2181. if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
  2182. umode_t mode = op->mode;
  2183. if (!IS_POSIXACL(dir->d_inode))
  2184. mode &= ~current_umask();
  2185. /*
  2186. * This write is needed to ensure that a
  2187. * rw->ro transition does not occur between
  2188. * the time when the file is created and when
  2189. * a permanent write count is taken through
  2190. * the 'struct file' in finish_open().
  2191. */
  2192. error = mnt_want_write(nd->path.mnt);
  2193. if (error)
  2194. goto out_dput;
  2195. *want_write = true;
  2196. *opened |= FILE_CREATED;
  2197. error = security_path_mknod(&nd->path, dentry, mode, 0);
  2198. if (error)
  2199. goto out_dput;
  2200. error = vfs_create(dir->d_inode, dentry, mode,
  2201. nd->flags & LOOKUP_EXCL);
  2202. if (error)
  2203. goto out_dput;
  2204. }
  2205. out_no_open:
  2206. path->dentry = dentry;
  2207. path->mnt = nd->path.mnt;
  2208. return 1;
  2209. out_dput:
  2210. dput(dentry);
  2211. return error;
  2212. }
  2213. /*
  2214. * Handle the last step of open()
  2215. */
  2216. static int do_last(struct nameidata *nd, struct path *path,
  2217. struct file *file, const struct open_flags *op,
  2218. int *opened, const char *pathname)
  2219. {
  2220. struct dentry *dir = nd->path.dentry;
  2221. int open_flag = op->open_flag;
  2222. bool will_truncate = (open_flag & O_TRUNC) != 0;
  2223. bool want_write = false;
  2224. int acc_mode = op->acc_mode;
  2225. struct inode *inode;
  2226. bool symlink_ok = false;
  2227. struct path save_parent = { .dentry = NULL, .mnt = NULL };
  2228. bool retried = false;
  2229. int error;
  2230. nd->flags &= ~LOOKUP_PARENT;
  2231. nd->flags |= op->intent;
  2232. switch (nd->last_type) {
  2233. case LAST_DOTDOT:
  2234. case LAST_DOT:
  2235. error = handle_dots(nd, nd->last_type);
  2236. if (error)
  2237. return error;
  2238. /* fallthrough */
  2239. case LAST_ROOT:
  2240. error = complete_walk(nd);
  2241. if (error)
  2242. return error;
  2243. audit_inode(pathname, nd->path.dentry);
  2244. if (open_flag & O_CREAT) {
  2245. error = -EISDIR;
  2246. goto out;
  2247. }
  2248. goto finish_open;
  2249. case LAST_BIND:
  2250. error = complete_walk(nd);
  2251. if (error)
  2252. return error;
  2253. audit_inode(pathname, dir);
  2254. goto finish_open;
  2255. }
  2256. if (!(open_flag & O_CREAT)) {
  2257. if (nd->last.name[nd->last.len])
  2258. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2259. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  2260. symlink_ok = true;
  2261. /* we _can_ be in RCU mode here */
  2262. error = lookup_fast(nd, &nd->last, path, &inode);
  2263. if (likely(!error))
  2264. goto finish_lookup;
  2265. if (error < 0)
  2266. goto out;
  2267. BUG_ON(nd->inode != dir->d_inode);
  2268. } else {
  2269. /* create side of things */
  2270. /*
  2271. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
  2272. * has been cleared when we got to the last component we are
  2273. * about to look up
  2274. */
  2275. error = complete_walk(nd);
  2276. if (error)
  2277. return error;
  2278. audit_inode(pathname, dir);
  2279. error = -EISDIR;
  2280. /* trailing slashes? */
  2281. if (nd->last.name[nd->last.len])
  2282. goto out;
  2283. }
  2284. retry_lookup:
  2285. mutex_lock(&dir->d_inode->i_mutex);
  2286. error = lookup_open(nd, path, file, op, &want_write, opened);
  2287. mutex_unlock(&dir->d_inode->i_mutex);
  2288. if (error <= 0) {
  2289. if (error)
  2290. goto out;
  2291. if ((*opened & FILE_CREATED) ||
  2292. !S_ISREG(file->f_path.dentry->d_inode->i_mode))
  2293. will_truncate = false;
  2294. audit_inode(pathname, file->f_path.dentry);
  2295. goto opened;
  2296. }
  2297. if (*opened & FILE_CREATED) {
  2298. /* Don't check for write permission, don't truncate */
  2299. open_flag &= ~O_TRUNC;
  2300. will_truncate = false;
  2301. acc_mode = MAY_OPEN;
  2302. path_to_nameidata(path, nd);
  2303. goto finish_open_created;
  2304. }
  2305. /*
  2306. * It already exists.
  2307. */
  2308. audit_inode(pathname, path->dentry);
  2309. /*
  2310. * If atomic_open() acquired write access it is dropped now due to
  2311. * possible mount and symlink following (this might be optimized away if
  2312. * necessary...)
  2313. */
  2314. if (want_write) {
  2315. mnt_drop_write(nd->path.mnt);
  2316. want_write = false;
  2317. }
  2318. error = -EEXIST;
  2319. if (open_flag & O_EXCL)
  2320. goto exit_dput;
  2321. error = follow_managed(path, nd->flags);
  2322. if (error < 0)
  2323. goto exit_dput;
  2324. if (error)
  2325. nd->flags |= LOOKUP_JUMPED;
  2326. BUG_ON(nd->flags & LOOKUP_RCU);
  2327. inode = path->dentry->d_inode;
  2328. finish_lookup:
  2329. /* we _can_ be in RCU mode here */
  2330. error = -ENOENT;
  2331. if (!inode) {
  2332. path_to_nameidata(path, nd);
  2333. goto out;
  2334. }
  2335. if (should_follow_link(inode, !symlink_ok)) {
  2336. if (nd->flags & LOOKUP_RCU) {
  2337. if (unlikely(unlazy_walk(nd, path->dentry))) {
  2338. error = -ECHILD;
  2339. goto out;
  2340. }
  2341. }
  2342. BUG_ON(inode != path->dentry->d_inode);
  2343. return 1;
  2344. }
  2345. if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
  2346. path_to_nameidata(path, nd);
  2347. } else {
  2348. save_parent.dentry = nd->path.dentry;
  2349. save_parent.mnt = mntget(path->mnt);
  2350. nd->path.dentry = path->dentry;
  2351. }
  2352. nd->inode = inode;
  2353. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2354. error = complete_walk(nd);
  2355. if (error) {
  2356. path_put(&save_parent);
  2357. return error;
  2358. }
  2359. error = -EISDIR;
  2360. if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
  2361. goto out;
  2362. error = -ENOTDIR;
  2363. if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
  2364. goto out;
  2365. audit_inode(pathname, nd->path.dentry);
  2366. finish_open:
  2367. if (!S_ISREG(nd->inode->i_mode))
  2368. will_truncate = false;
  2369. if (will_truncate) {
  2370. error = mnt_want_write(nd->path.mnt);
  2371. if (error)
  2372. goto out;
  2373. want_write = true;
  2374. }
  2375. finish_open_created:
  2376. error = may_open(&nd->path, acc_mode, open_flag);
  2377. if (error)
  2378. goto out;
  2379. file->f_path.mnt = nd->path.mnt;
  2380. error = finish_open(file, nd->path.dentry, NULL, opened);
  2381. if (error) {
  2382. if (error == -EOPENSTALE)
  2383. goto stale_open;
  2384. goto out;
  2385. }
  2386. opened:
  2387. error = open_check_o_direct(file);
  2388. if (error)
  2389. goto exit_fput;
  2390. error = ima_file_check(file, op->acc_mode);
  2391. if (error)
  2392. goto exit_fput;
  2393. if (will_truncate) {
  2394. error = handle_truncate(file);
  2395. if (error)
  2396. goto exit_fput;
  2397. }
  2398. out:
  2399. if (want_write)
  2400. mnt_drop_write(nd->path.mnt);
  2401. path_put(&save_parent);
  2402. terminate_walk(nd);
  2403. return error;
  2404. exit_dput:
  2405. path_put_conditional(path, nd);
  2406. goto out;
  2407. exit_fput:
  2408. fput(file);
  2409. goto out;
  2410. stale_open:
  2411. /* If no saved parent or already retried then can't retry */
  2412. if (!save_parent.dentry || retried)
  2413. goto out;
  2414. BUG_ON(save_parent.dentry != dir);
  2415. path_put(&nd->path);
  2416. nd->path = save_parent;
  2417. nd->inode = dir->d_inode;
  2418. save_parent.mnt = NULL;
  2419. save_parent.dentry = NULL;
  2420. if (want_write) {
  2421. mnt_drop_write(nd->path.mnt);
  2422. want_write = false;
  2423. }
  2424. retried = true;
  2425. goto retry_lookup;
  2426. }
  2427. static struct file *path_openat(int dfd, const char *pathname,
  2428. struct nameidata *nd, const struct open_flags *op, int flags)
  2429. {
  2430. struct file *base = NULL;
  2431. struct file *file;
  2432. struct path path;
  2433. int opened = 0;
  2434. int error;
  2435. file = get_empty_filp();
  2436. if (!file)
  2437. return ERR_PTR(-ENFILE);
  2438. file->f_flags = op->open_flag;
  2439. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  2440. if (unlikely(error))
  2441. goto out;
  2442. current->total_link_count = 0;
  2443. error = link_path_walk(pathname, nd);
  2444. if (unlikely(error))
  2445. goto out;
  2446. error = do_last(nd, &path, file, op, &opened, pathname);
  2447. while (unlikely(error > 0)) { /* trailing symlink */
  2448. struct path link = path;
  2449. void *cookie;
  2450. if (!(nd->flags & LOOKUP_FOLLOW)) {
  2451. path_put_conditional(&path, nd);
  2452. path_put(&nd->path);
  2453. error = -ELOOP;
  2454. break;
  2455. }
  2456. nd->flags |= LOOKUP_PARENT;
  2457. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2458. error = follow_link(&link, nd, &cookie);
  2459. if (unlikely(error))
  2460. break;
  2461. error = do_last(nd, &path, file, op, &opened, pathname);
  2462. put_link(nd, &link, cookie);
  2463. }
  2464. out:
  2465. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2466. path_put(&nd->root);
  2467. if (base)
  2468. fput(base);
  2469. if (!(opened & FILE_OPENED)) {
  2470. BUG_ON(!error);
  2471. put_filp(file);
  2472. }
  2473. if (unlikely(error)) {
  2474. if (error == -EOPENSTALE) {
  2475. if (flags & LOOKUP_RCU)
  2476. error = -ECHILD;
  2477. else
  2478. error = -ESTALE;
  2479. }
  2480. file = ERR_PTR(error);
  2481. }
  2482. return file;
  2483. }
  2484. struct file *do_filp_open(int dfd, const char *pathname,
  2485. const struct open_flags *op, int flags)
  2486. {
  2487. struct nameidata nd;
  2488. struct file *filp;
  2489. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2490. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2491. filp = path_openat(dfd, pathname, &nd, op, flags);
  2492. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2493. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2494. return filp;
  2495. }
  2496. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2497. const char *name, const struct open_flags *op, int flags)
  2498. {
  2499. struct nameidata nd;
  2500. struct file *file;
  2501. nd.root.mnt = mnt;
  2502. nd.root.dentry = dentry;
  2503. flags |= LOOKUP_ROOT;
  2504. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2505. return ERR_PTR(-ELOOP);
  2506. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2507. if (unlikely(file == ERR_PTR(-ECHILD)))
  2508. file = path_openat(-1, name, &nd, op, flags);
  2509. if (unlikely(file == ERR_PTR(-ESTALE)))
  2510. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2511. return file;
  2512. }
  2513. struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
  2514. {
  2515. struct dentry *dentry = ERR_PTR(-EEXIST);
  2516. struct nameidata nd;
  2517. int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
  2518. if (error)
  2519. return ERR_PTR(error);
  2520. /*
  2521. * Yucky last component or no last component at all?
  2522. * (foo/., foo/.., /////)
  2523. */
  2524. if (nd.last_type != LAST_NORM)
  2525. goto out;
  2526. nd.flags &= ~LOOKUP_PARENT;
  2527. nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2528. /*
  2529. * Do the final lookup.
  2530. */
  2531. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2532. dentry = lookup_hash(&nd);
  2533. if (IS_ERR(dentry))
  2534. goto fail;
  2535. if (dentry->d_inode)
  2536. goto eexist;
  2537. /*
  2538. * Special case - lookup gave negative, but... we had foo/bar/
  2539. * From the vfs_mknod() POV we just have a negative dentry -
  2540. * all is fine. Let's be bastards - you had / on the end, you've
  2541. * been asking for (non-existent) directory. -ENOENT for you.
  2542. */
  2543. if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
  2544. dput(dentry);
  2545. dentry = ERR_PTR(-ENOENT);
  2546. goto fail;
  2547. }
  2548. *path = nd.path;
  2549. return dentry;
  2550. eexist:
  2551. dput(dentry);
  2552. dentry = ERR_PTR(-EEXIST);
  2553. fail:
  2554. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2555. out:
  2556. path_put(&nd.path);
  2557. return dentry;
  2558. }
  2559. EXPORT_SYMBOL(kern_path_create);
  2560. void done_path_create(struct path *path, struct dentry *dentry)
  2561. {
  2562. dput(dentry);
  2563. mutex_unlock(&path->dentry->d_inode->i_mutex);
  2564. path_put(path);
  2565. }
  2566. EXPORT_SYMBOL(done_path_create);
  2567. struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
  2568. {
  2569. char *tmp = getname(pathname);
  2570. struct dentry *res;
  2571. if (IS_ERR(tmp))
  2572. return ERR_CAST(tmp);
  2573. res = kern_path_create(dfd, tmp, path, is_dir);
  2574. putname(tmp);
  2575. return res;
  2576. }
  2577. EXPORT_SYMBOL(user_path_create);
  2578. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2579. {
  2580. int error = may_create(dir, dentry);
  2581. if (error)
  2582. return error;
  2583. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  2584. return -EPERM;
  2585. if (!dir->i_op->mknod)
  2586. return -EPERM;
  2587. error = devcgroup_inode_mknod(mode, dev);
  2588. if (error)
  2589. return error;
  2590. error = security_inode_mknod(dir, dentry, mode, dev);
  2591. if (error)
  2592. return error;
  2593. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2594. if (!error)
  2595. fsnotify_create(dir, dentry);
  2596. return error;
  2597. }
  2598. static int may_mknod(umode_t mode)
  2599. {
  2600. switch (mode & S_IFMT) {
  2601. case S_IFREG:
  2602. case S_IFCHR:
  2603. case S_IFBLK:
  2604. case S_IFIFO:
  2605. case S_IFSOCK:
  2606. case 0: /* zero mode translates to S_IFREG */
  2607. return 0;
  2608. case S_IFDIR:
  2609. return -EPERM;
  2610. default:
  2611. return -EINVAL;
  2612. }
  2613. }
  2614. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  2615. unsigned, dev)
  2616. {
  2617. struct dentry *dentry;
  2618. struct path path;
  2619. int error;
  2620. if (S_ISDIR(mode))
  2621. return -EPERM;
  2622. dentry = user_path_create(dfd, filename, &path, 0);
  2623. if (IS_ERR(dentry))
  2624. return PTR_ERR(dentry);
  2625. if (!IS_POSIXACL(path.dentry->d_inode))
  2626. mode &= ~current_umask();
  2627. error = may_mknod(mode);
  2628. if (error)
  2629. goto out_dput;
  2630. error = mnt_want_write(path.mnt);
  2631. if (error)
  2632. goto out_dput;
  2633. error = security_path_mknod(&path, dentry, mode, dev);
  2634. if (error)
  2635. goto out_drop_write;
  2636. switch (mode & S_IFMT) {
  2637. case 0: case S_IFREG:
  2638. error = vfs_create(path.dentry->d_inode,dentry,mode,true);
  2639. break;
  2640. case S_IFCHR: case S_IFBLK:
  2641. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  2642. new_decode_dev(dev));
  2643. break;
  2644. case S_IFIFO: case S_IFSOCK:
  2645. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  2646. break;
  2647. }
  2648. out_drop_write:
  2649. mnt_drop_write(path.mnt);
  2650. out_dput:
  2651. done_path_create(&path, dentry);
  2652. return error;
  2653. }
  2654. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  2655. {
  2656. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2657. }
  2658. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  2659. {
  2660. int error = may_create(dir, dentry);
  2661. unsigned max_links = dir->i_sb->s_max_links;
  2662. if (error)
  2663. return error;
  2664. if (!dir->i_op->mkdir)
  2665. return -EPERM;
  2666. mode &= (S_IRWXUGO|S_ISVTX);
  2667. error = security_inode_mkdir(dir, dentry, mode);
  2668. if (error)
  2669. return error;
  2670. if (max_links && dir->i_nlink >= max_links)
  2671. return -EMLINK;
  2672. error = dir->i_op->mkdir(dir, dentry, mode);
  2673. if (!error)
  2674. fsnotify_mkdir(dir, dentry);
  2675. return error;
  2676. }
  2677. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  2678. {
  2679. struct dentry *dentry;
  2680. struct path path;
  2681. int error;
  2682. dentry = user_path_create(dfd, pathname, &path, 1);
  2683. if (IS_ERR(dentry))
  2684. return PTR_ERR(dentry);
  2685. if (!IS_POSIXACL(path.dentry->d_inode))
  2686. mode &= ~current_umask();
  2687. error = mnt_want_write(path.mnt);
  2688. if (error)
  2689. goto out_dput;
  2690. error = security_path_mkdir(&path, dentry, mode);
  2691. if (error)
  2692. goto out_drop_write;
  2693. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  2694. out_drop_write:
  2695. mnt_drop_write(path.mnt);
  2696. out_dput:
  2697. done_path_create(&path, dentry);
  2698. return error;
  2699. }
  2700. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  2701. {
  2702. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2703. }
  2704. /*
  2705. * The dentry_unhash() helper will try to drop the dentry early: we
  2706. * should have a usage count of 1 if we're the only user of this
  2707. * dentry, and if that is true (possibly after pruning the dcache),
  2708. * then we drop the dentry now.
  2709. *
  2710. * A low-level filesystem can, if it choses, legally
  2711. * do a
  2712. *
  2713. * if (!d_unhashed(dentry))
  2714. * return -EBUSY;
  2715. *
  2716. * if it cannot handle the case of removing a directory
  2717. * that is still in use by something else..
  2718. */
  2719. void dentry_unhash(struct dentry *dentry)
  2720. {
  2721. shrink_dcache_parent(dentry);
  2722. spin_lock(&dentry->d_lock);
  2723. if (dentry->d_count == 1)
  2724. __d_drop(dentry);
  2725. spin_unlock(&dentry->d_lock);
  2726. }
  2727. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2728. {
  2729. int error = may_delete(dir, dentry, 1);
  2730. if (error)
  2731. return error;
  2732. if (!dir->i_op->rmdir)
  2733. return -EPERM;
  2734. dget(dentry);
  2735. mutex_lock(&dentry->d_inode->i_mutex);
  2736. error = -EBUSY;
  2737. if (d_mountpoint(dentry))
  2738. goto out;
  2739. error = security_inode_rmdir(dir, dentry);
  2740. if (error)
  2741. goto out;
  2742. shrink_dcache_parent(dentry);
  2743. error = dir->i_op->rmdir(dir, dentry);
  2744. if (error)
  2745. goto out;
  2746. dentry->d_inode->i_flags |= S_DEAD;
  2747. dont_mount(dentry);
  2748. out:
  2749. mutex_unlock(&dentry->d_inode->i_mutex);
  2750. dput(dentry);
  2751. if (!error)
  2752. d_delete(dentry);
  2753. return error;
  2754. }
  2755. static long do_rmdir(int dfd, const char __user *pathname)
  2756. {
  2757. int error = 0;
  2758. char * name;
  2759. struct dentry *dentry;
  2760. struct nameidata nd;
  2761. error = user_path_parent(dfd, pathname, &nd, &name);
  2762. if (error)
  2763. return error;
  2764. switch(nd.last_type) {
  2765. case LAST_DOTDOT:
  2766. error = -ENOTEMPTY;
  2767. goto exit1;
  2768. case LAST_DOT:
  2769. error = -EINVAL;
  2770. goto exit1;
  2771. case LAST_ROOT:
  2772. error = -EBUSY;
  2773. goto exit1;
  2774. }
  2775. nd.flags &= ~LOOKUP_PARENT;
  2776. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2777. dentry = lookup_hash(&nd);
  2778. error = PTR_ERR(dentry);
  2779. if (IS_ERR(dentry))
  2780. goto exit2;
  2781. if (!dentry->d_inode) {
  2782. error = -ENOENT;
  2783. goto exit3;
  2784. }
  2785. error = mnt_want_write(nd.path.mnt);
  2786. if (error)
  2787. goto exit3;
  2788. error = security_path_rmdir(&nd.path, dentry);
  2789. if (error)
  2790. goto exit4;
  2791. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2792. exit4:
  2793. mnt_drop_write(nd.path.mnt);
  2794. exit3:
  2795. dput(dentry);
  2796. exit2:
  2797. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2798. exit1:
  2799. path_put(&nd.path);
  2800. putname(name);
  2801. return error;
  2802. }
  2803. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2804. {
  2805. return do_rmdir(AT_FDCWD, pathname);
  2806. }
  2807. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2808. {
  2809. int error = may_delete(dir, dentry, 0);
  2810. if (error)
  2811. return error;
  2812. if (!dir->i_op->unlink)
  2813. return -EPERM;
  2814. mutex_lock(&dentry->d_inode->i_mutex);
  2815. if (d_mountpoint(dentry))
  2816. error = -EBUSY;
  2817. else {
  2818. error = security_inode_unlink(dir, dentry);
  2819. if (!error) {
  2820. error = dir->i_op->unlink(dir, dentry);
  2821. if (!error)
  2822. dont_mount(dentry);
  2823. }
  2824. }
  2825. mutex_unlock(&dentry->d_inode->i_mutex);
  2826. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2827. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2828. fsnotify_link_count(dentry->d_inode);
  2829. d_delete(dentry);
  2830. }
  2831. return error;
  2832. }
  2833. /*
  2834. * Make sure that the actual truncation of the file will occur outside its
  2835. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2836. * writeout happening, and we don't want to prevent access to the directory
  2837. * while waiting on the I/O.
  2838. */
  2839. static long do_unlinkat(int dfd, const char __user *pathname)
  2840. {
  2841. int error;
  2842. char *name;
  2843. struct dentry *dentry;
  2844. struct nameidata nd;
  2845. struct inode *inode = NULL;
  2846. error = user_path_parent(dfd, pathname, &nd, &name);
  2847. if (error)
  2848. return error;
  2849. error = -EISDIR;
  2850. if (nd.last_type != LAST_NORM)
  2851. goto exit1;
  2852. nd.flags &= ~LOOKUP_PARENT;
  2853. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2854. dentry = lookup_hash(&nd);
  2855. error = PTR_ERR(dentry);
  2856. if (!IS_ERR(dentry)) {
  2857. /* Why not before? Because we want correct error value */
  2858. if (nd.last.name[nd.last.len])
  2859. goto slashes;
  2860. inode = dentry->d_inode;
  2861. if (!inode)
  2862. goto slashes;
  2863. ihold(inode);
  2864. error = mnt_want_write(nd.path.mnt);
  2865. if (error)
  2866. goto exit2;
  2867. error = security_path_unlink(&nd.path, dentry);
  2868. if (error)
  2869. goto exit3;
  2870. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2871. exit3:
  2872. mnt_drop_write(nd.path.mnt);
  2873. exit2:
  2874. dput(dentry);
  2875. }
  2876. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2877. if (inode)
  2878. iput(inode); /* truncate the inode here */
  2879. exit1:
  2880. path_put(&nd.path);
  2881. putname(name);
  2882. return error;
  2883. slashes:
  2884. error = !dentry->d_inode ? -ENOENT :
  2885. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2886. goto exit2;
  2887. }
  2888. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2889. {
  2890. if ((flag & ~AT_REMOVEDIR) != 0)
  2891. return -EINVAL;
  2892. if (flag & AT_REMOVEDIR)
  2893. return do_rmdir(dfd, pathname);
  2894. return do_unlinkat(dfd, pathname);
  2895. }
  2896. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2897. {
  2898. return do_unlinkat(AT_FDCWD, pathname);
  2899. }
  2900. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2901. {
  2902. int error = may_create(dir, dentry);
  2903. if (error)
  2904. return error;
  2905. if (!dir->i_op->symlink)
  2906. return -EPERM;
  2907. error = security_inode_symlink(dir, dentry, oldname);
  2908. if (error)
  2909. return error;
  2910. error = dir->i_op->symlink(dir, dentry, oldname);
  2911. if (!error)
  2912. fsnotify_create(dir, dentry);
  2913. return error;
  2914. }
  2915. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2916. int, newdfd, const char __user *, newname)
  2917. {
  2918. int error;
  2919. char *from;
  2920. struct dentry *dentry;
  2921. struct path path;
  2922. from = getname(oldname);
  2923. if (IS_ERR(from))
  2924. return PTR_ERR(from);
  2925. dentry = user_path_create(newdfd, newname, &path, 0);
  2926. error = PTR_ERR(dentry);
  2927. if (IS_ERR(dentry))
  2928. goto out_putname;
  2929. error = mnt_want_write(path.mnt);
  2930. if (error)
  2931. goto out_dput;
  2932. error = security_path_symlink(&path, dentry, from);
  2933. if (error)
  2934. goto out_drop_write;
  2935. error = vfs_symlink(path.dentry->d_inode, dentry, from);
  2936. out_drop_write:
  2937. mnt_drop_write(path.mnt);
  2938. out_dput:
  2939. done_path_create(&path, dentry);
  2940. out_putname:
  2941. putname(from);
  2942. return error;
  2943. }
  2944. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2945. {
  2946. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2947. }
  2948. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2949. {
  2950. struct inode *inode = old_dentry->d_inode;
  2951. unsigned max_links = dir->i_sb->s_max_links;
  2952. int error;
  2953. if (!inode)
  2954. return -ENOENT;
  2955. error = may_create(dir, new_dentry);
  2956. if (error)
  2957. return error;
  2958. if (dir->i_sb != inode->i_sb)
  2959. return -EXDEV;
  2960. /*
  2961. * A link to an append-only or immutable file cannot be created.
  2962. */
  2963. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2964. return -EPERM;
  2965. if (!dir->i_op->link)
  2966. return -EPERM;
  2967. if (S_ISDIR(inode->i_mode))
  2968. return -EPERM;
  2969. error = security_inode_link(old_dentry, dir, new_dentry);
  2970. if (error)
  2971. return error;
  2972. mutex_lock(&inode->i_mutex);
  2973. /* Make sure we don't allow creating hardlink to an unlinked file */
  2974. if (inode->i_nlink == 0)
  2975. error = -ENOENT;
  2976. else if (max_links && inode->i_nlink >= max_links)
  2977. error = -EMLINK;
  2978. else
  2979. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2980. mutex_unlock(&inode->i_mutex);
  2981. if (!error)
  2982. fsnotify_link(dir, inode, new_dentry);
  2983. return error;
  2984. }
  2985. /*
  2986. * Hardlinks are often used in delicate situations. We avoid
  2987. * security-related surprises by not following symlinks on the
  2988. * newname. --KAB
  2989. *
  2990. * We don't follow them on the oldname either to be compatible
  2991. * with linux 2.0, and to avoid hard-linking to directories
  2992. * and other special files. --ADM
  2993. */
  2994. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2995. int, newdfd, const char __user *, newname, int, flags)
  2996. {
  2997. struct dentry *new_dentry;
  2998. struct path old_path, new_path;
  2999. int how = 0;
  3000. int error;
  3001. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  3002. return -EINVAL;
  3003. /*
  3004. * To use null names we require CAP_DAC_READ_SEARCH
  3005. * This ensures that not everyone will be able to create
  3006. * handlink using the passed filedescriptor.
  3007. */
  3008. if (flags & AT_EMPTY_PATH) {
  3009. if (!capable(CAP_DAC_READ_SEARCH))
  3010. return -ENOENT;
  3011. how = LOOKUP_EMPTY;
  3012. }
  3013. if (flags & AT_SYMLINK_FOLLOW)
  3014. how |= LOOKUP_FOLLOW;
  3015. error = user_path_at(olddfd, oldname, how, &old_path);
  3016. if (error)
  3017. return error;
  3018. new_dentry = user_path_create(newdfd, newname, &new_path, 0);
  3019. error = PTR_ERR(new_dentry);
  3020. if (IS_ERR(new_dentry))
  3021. goto out;
  3022. error = -EXDEV;
  3023. if (old_path.mnt != new_path.mnt)
  3024. goto out_dput;
  3025. error = mnt_want_write(new_path.mnt);
  3026. if (error)
  3027. goto out_dput;
  3028. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  3029. if (error)
  3030. goto out_drop_write;
  3031. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
  3032. out_drop_write:
  3033. mnt_drop_write(new_path.mnt);
  3034. out_dput:
  3035. done_path_create(&new_path, new_dentry);
  3036. out:
  3037. path_put(&old_path);
  3038. return error;
  3039. }
  3040. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  3041. {
  3042. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3043. }
  3044. /*
  3045. * The worst of all namespace operations - renaming directory. "Perverted"
  3046. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  3047. * Problems:
  3048. * a) we can get into loop creation. Check is done in is_subdir().
  3049. * b) race potential - two innocent renames can create a loop together.
  3050. * That's where 4.4 screws up. Current fix: serialization on
  3051. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  3052. * story.
  3053. * c) we have to lock _three_ objects - parents and victim (if it exists).
  3054. * And that - after we got ->i_mutex on parents (until then we don't know
  3055. * whether the target exists). Solution: try to be smart with locking
  3056. * order for inodes. We rely on the fact that tree topology may change
  3057. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  3058. * move will be locked. Thus we can rank directories by the tree
  3059. * (ancestors first) and rank all non-directories after them.
  3060. * That works since everybody except rename does "lock parent, lookup,
  3061. * lock child" and rename is under ->s_vfs_rename_mutex.
  3062. * HOWEVER, it relies on the assumption that any object with ->lookup()
  3063. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  3064. * we'd better make sure that there's no link(2) for them.
  3065. * d) conversion from fhandle to dentry may come in the wrong moment - when
  3066. * we are removing the target. Solution: we will have to grab ->i_mutex
  3067. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  3068. * ->i_mutex on parents, which works but leads to some truly excessive
  3069. * locking].
  3070. */
  3071. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  3072. struct inode *new_dir, struct dentry *new_dentry)
  3073. {
  3074. int error = 0;
  3075. struct inode *target = new_dentry->d_inode;
  3076. unsigned max_links = new_dir->i_sb->s_max_links;
  3077. /*
  3078. * If we are going to change the parent - check write permissions,
  3079. * we'll need to flip '..'.
  3080. */
  3081. if (new_dir != old_dir) {
  3082. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  3083. if (error)
  3084. return error;
  3085. }
  3086. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  3087. if (error)
  3088. return error;
  3089. dget(new_dentry);
  3090. if (target)
  3091. mutex_lock(&target->i_mutex);
  3092. error = -EBUSY;
  3093. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  3094. goto out;
  3095. error = -EMLINK;
  3096. if (max_links && !target && new_dir != old_dir &&
  3097. new_dir->i_nlink >= max_links)
  3098. goto out;
  3099. if (target)
  3100. shrink_dcache_parent(new_dentry);
  3101. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  3102. if (error)
  3103. goto out;
  3104. if (target) {
  3105. target->i_flags |= S_DEAD;
  3106. dont_mount(new_dentry);
  3107. }
  3108. out:
  3109. if (target)
  3110. mutex_unlock(&target->i_mutex);
  3111. dput(new_dentry);
  3112. if (!error)
  3113. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  3114. d_move(old_dentry,new_dentry);
  3115. return error;
  3116. }
  3117. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  3118. struct inode *new_dir, struct dentry *new_dentry)
  3119. {
  3120. struct inode *target = new_dentry->d_inode;
  3121. int error;
  3122. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  3123. if (error)
  3124. return error;
  3125. dget(new_dentry);
  3126. if (target)
  3127. mutex_lock(&target->i_mutex);
  3128. error = -EBUSY;
  3129. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  3130. goto out;
  3131. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  3132. if (error)
  3133. goto out;
  3134. if (target)
  3135. dont_mount(new_dentry);
  3136. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  3137. d_move(old_dentry, new_dentry);
  3138. out:
  3139. if (target)
  3140. mutex_unlock(&target->i_mutex);
  3141. dput(new_dentry);
  3142. return error;
  3143. }
  3144. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  3145. struct inode *new_dir, struct dentry *new_dentry)
  3146. {
  3147. int error;
  3148. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  3149. const unsigned char *old_name;
  3150. if (old_dentry->d_inode == new_dentry->d_inode)
  3151. return 0;
  3152. error = may_delete(old_dir, old_dentry, is_dir);
  3153. if (error)
  3154. return error;
  3155. if (!new_dentry->d_inode)
  3156. error = may_create(new_dir, new_dentry);
  3157. else
  3158. error = may_delete(new_dir, new_dentry, is_dir);
  3159. if (error)
  3160. return error;
  3161. if (!old_dir->i_op->rename)
  3162. return -EPERM;
  3163. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  3164. if (is_dir)
  3165. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  3166. else
  3167. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  3168. if (!error)
  3169. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  3170. new_dentry->d_inode, old_dentry);
  3171. fsnotify_oldname_free(old_name);
  3172. return error;
  3173. }
  3174. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  3175. int, newdfd, const char __user *, newname)
  3176. {
  3177. struct dentry *old_dir, *new_dir;
  3178. struct dentry *old_dentry, *new_dentry;
  3179. struct dentry *trap;
  3180. struct nameidata oldnd, newnd;
  3181. char *from;
  3182. char *to;
  3183. int error;
  3184. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  3185. if (error)
  3186. goto exit;
  3187. error = user_path_parent(newdfd, newname, &newnd, &to);
  3188. if (error)
  3189. goto exit1;
  3190. error = -EXDEV;
  3191. if (oldnd.path.mnt != newnd.path.mnt)
  3192. goto exit2;
  3193. old_dir = oldnd.path.dentry;
  3194. error = -EBUSY;
  3195. if (oldnd.last_type != LAST_NORM)
  3196. goto exit2;
  3197. new_dir = newnd.path.dentry;
  3198. if (newnd.last_type != LAST_NORM)
  3199. goto exit2;
  3200. oldnd.flags &= ~LOOKUP_PARENT;
  3201. newnd.flags &= ~LOOKUP_PARENT;
  3202. newnd.flags |= LOOKUP_RENAME_TARGET;
  3203. trap = lock_rename(new_dir, old_dir);
  3204. old_dentry = lookup_hash(&oldnd);
  3205. error = PTR_ERR(old_dentry);
  3206. if (IS_ERR(old_dentry))
  3207. goto exit3;
  3208. /* source must exist */
  3209. error = -ENOENT;
  3210. if (!old_dentry->d_inode)
  3211. goto exit4;
  3212. /* unless the source is a directory trailing slashes give -ENOTDIR */
  3213. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  3214. error = -ENOTDIR;
  3215. if (oldnd.last.name[oldnd.last.len])
  3216. goto exit4;
  3217. if (newnd.last.name[newnd.last.len])
  3218. goto exit4;
  3219. }
  3220. /* source should not be ancestor of target */
  3221. error = -EINVAL;
  3222. if (old_dentry == trap)
  3223. goto exit4;
  3224. new_dentry = lookup_hash(&newnd);
  3225. error = PTR_ERR(new_dentry);
  3226. if (IS_ERR(new_dentry))
  3227. goto exit4;
  3228. /* target should not be an ancestor of source */
  3229. error = -ENOTEMPTY;
  3230. if (new_dentry == trap)
  3231. goto exit5;
  3232. error = mnt_want_write(oldnd.path.mnt);
  3233. if (error)
  3234. goto exit5;
  3235. error = security_path_rename(&oldnd.path, old_dentry,
  3236. &newnd.path, new_dentry);
  3237. if (error)
  3238. goto exit6;
  3239. error = vfs_rename(old_dir->d_inode, old_dentry,
  3240. new_dir->d_inode, new_dentry);
  3241. exit6:
  3242. mnt_drop_write(oldnd.path.mnt);
  3243. exit5:
  3244. dput(new_dentry);
  3245. exit4:
  3246. dput(old_dentry);
  3247. exit3:
  3248. unlock_rename(new_dir, old_dir);
  3249. exit2:
  3250. path_put(&newnd.path);
  3251. putname(to);
  3252. exit1:
  3253. path_put(&oldnd.path);
  3254. putname(from);
  3255. exit:
  3256. return error;
  3257. }
  3258. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  3259. {
  3260. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  3261. }
  3262. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  3263. {
  3264. int len;
  3265. len = PTR_ERR(link);
  3266. if (IS_ERR(link))
  3267. goto out;
  3268. len = strlen(link);
  3269. if (len > (unsigned) buflen)
  3270. len = buflen;
  3271. if (copy_to_user(buffer, link, len))
  3272. len = -EFAULT;
  3273. out:
  3274. return len;
  3275. }
  3276. /*
  3277. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  3278. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  3279. * using) it for any given inode is up to filesystem.
  3280. */
  3281. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3282. {
  3283. struct nameidata nd;
  3284. void *cookie;
  3285. int res;
  3286. nd.depth = 0;
  3287. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  3288. if (IS_ERR(cookie))
  3289. return PTR_ERR(cookie);
  3290. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  3291. if (dentry->d_inode->i_op->put_link)
  3292. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  3293. return res;
  3294. }
  3295. int vfs_follow_link(struct nameidata *nd, const char *link)
  3296. {
  3297. return __vfs_follow_link(nd, link);
  3298. }
  3299. /* get the link contents into pagecache */
  3300. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  3301. {
  3302. char *kaddr;
  3303. struct page *page;
  3304. struct address_space *mapping = dentry->d_inode->i_mapping;
  3305. page = read_mapping_page(mapping, 0, NULL);
  3306. if (IS_ERR(page))
  3307. return (char*)page;
  3308. *ppage = page;
  3309. kaddr = kmap(page);
  3310. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  3311. return kaddr;
  3312. }
  3313. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3314. {
  3315. struct page *page = NULL;
  3316. char *s = page_getlink(dentry, &page);
  3317. int res = vfs_readlink(dentry,buffer,buflen,s);
  3318. if (page) {
  3319. kunmap(page);
  3320. page_cache_release(page);
  3321. }
  3322. return res;
  3323. }
  3324. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  3325. {
  3326. struct page *page = NULL;
  3327. nd_set_link(nd, page_getlink(dentry, &page));
  3328. return page;
  3329. }
  3330. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  3331. {
  3332. struct page *page = cookie;
  3333. if (page) {
  3334. kunmap(page);
  3335. page_cache_release(page);
  3336. }
  3337. }
  3338. /*
  3339. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  3340. */
  3341. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  3342. {
  3343. struct address_space *mapping = inode->i_mapping;
  3344. struct page *page;
  3345. void *fsdata;
  3346. int err;
  3347. char *kaddr;
  3348. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  3349. if (nofs)
  3350. flags |= AOP_FLAG_NOFS;
  3351. retry:
  3352. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  3353. flags, &page, &fsdata);
  3354. if (err)
  3355. goto fail;
  3356. kaddr = kmap_atomic(page);
  3357. memcpy(kaddr, symname, len-1);
  3358. kunmap_atomic(kaddr);
  3359. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  3360. page, fsdata);
  3361. if (err < 0)
  3362. goto fail;
  3363. if (err < len-1)
  3364. goto retry;
  3365. mark_inode_dirty(inode);
  3366. return 0;
  3367. fail:
  3368. return err;
  3369. }
  3370. int page_symlink(struct inode *inode, const char *symname, int len)
  3371. {
  3372. return __page_symlink(inode, symname, len,
  3373. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  3374. }
  3375. const struct inode_operations page_symlink_inode_operations = {
  3376. .readlink = generic_readlink,
  3377. .follow_link = page_follow_link_light,
  3378. .put_link = page_put_link,
  3379. };
  3380. EXPORT_SYMBOL(user_path_at);
  3381. EXPORT_SYMBOL(follow_down_one);
  3382. EXPORT_SYMBOL(follow_down);
  3383. EXPORT_SYMBOL(follow_up);
  3384. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  3385. EXPORT_SYMBOL(getname);
  3386. EXPORT_SYMBOL(lock_rename);
  3387. EXPORT_SYMBOL(lookup_one_len);
  3388. EXPORT_SYMBOL(page_follow_link_light);
  3389. EXPORT_SYMBOL(page_put_link);
  3390. EXPORT_SYMBOL(page_readlink);
  3391. EXPORT_SYMBOL(__page_symlink);
  3392. EXPORT_SYMBOL(page_symlink);
  3393. EXPORT_SYMBOL(page_symlink_inode_operations);
  3394. EXPORT_SYMBOL(kern_path);
  3395. EXPORT_SYMBOL(vfs_path_lookup);
  3396. EXPORT_SYMBOL(inode_permission);
  3397. EXPORT_SYMBOL(unlock_rename);
  3398. EXPORT_SYMBOL(vfs_create);
  3399. EXPORT_SYMBOL(vfs_follow_link);
  3400. EXPORT_SYMBOL(vfs_link);
  3401. EXPORT_SYMBOL(vfs_mkdir);
  3402. EXPORT_SYMBOL(vfs_mknod);
  3403. EXPORT_SYMBOL(generic_permission);
  3404. EXPORT_SYMBOL(vfs_readlink);
  3405. EXPORT_SYMBOL(vfs_rename);
  3406. EXPORT_SYMBOL(vfs_rmdir);
  3407. EXPORT_SYMBOL(vfs_symlink);
  3408. EXPORT_SYMBOL(vfs_unlink);
  3409. EXPORT_SYMBOL(dentry_unhash);
  3410. EXPORT_SYMBOL(generic_readlink);