namei.c 115 KB

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