namei.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619
  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
  851. *
  852. * Returns 0 if successful, -ve on error.
  853. */
  854. static int may_linkat(struct path *link)
  855. {
  856. const struct cred *cred;
  857. struct inode *inode;
  858. if (!sysctl_protected_hardlinks)
  859. return 0;
  860. cred = current_cred();
  861. inode = link->dentry->d_inode;
  862. /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
  863. * otherwise, it must be a safe source.
  864. */
  865. if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
  866. capable(CAP_FOWNER))
  867. return 0;
  868. audit_log_link_denied("linkat", link);
  869. return -EPERM;
  870. }
  871. static __always_inline
  872. const char *get_link(struct nameidata *nd)
  873. {
  874. struct saved *last = nd->stack + nd->depth - 1;
  875. struct dentry *dentry = last->link.dentry;
  876. struct inode *inode = last->inode;
  877. int error;
  878. const char *res;
  879. if (!(nd->flags & LOOKUP_RCU)) {
  880. touch_atime(&last->link);
  881. cond_resched();
  882. } else if (atime_needs_update(&last->link, inode)) {
  883. if (unlikely(unlazy_walk(nd, NULL, 0)))
  884. return ERR_PTR(-ECHILD);
  885. touch_atime(&last->link);
  886. }
  887. error = security_inode_follow_link(dentry, inode,
  888. nd->flags & LOOKUP_RCU);
  889. if (unlikely(error))
  890. return ERR_PTR(error);
  891. nd->last_type = LAST_BIND;
  892. res = inode->i_link;
  893. if (!res) {
  894. if (nd->flags & LOOKUP_RCU) {
  895. if (unlikely(unlazy_walk(nd, NULL, 0)))
  896. return ERR_PTR(-ECHILD);
  897. }
  898. res = inode->i_op->follow_link(dentry, &last->cookie);
  899. if (IS_ERR_OR_NULL(res)) {
  900. last->cookie = NULL;
  901. return res;
  902. }
  903. }
  904. if (*res == '/') {
  905. if (nd->flags & LOOKUP_RCU) {
  906. struct dentry *d;
  907. if (!nd->root.mnt)
  908. set_root_rcu(nd);
  909. nd->path = nd->root;
  910. d = nd->path.dentry;
  911. nd->inode = d->d_inode;
  912. nd->seq = nd->root_seq;
  913. if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
  914. return ERR_PTR(-ECHILD);
  915. } else {
  916. if (!nd->root.mnt)
  917. set_root(nd);
  918. path_put(&nd->path);
  919. nd->path = nd->root;
  920. path_get(&nd->root);
  921. nd->inode = nd->path.dentry->d_inode;
  922. }
  923. nd->flags |= LOOKUP_JUMPED;
  924. while (unlikely(*++res == '/'))
  925. ;
  926. }
  927. if (!*res)
  928. res = NULL;
  929. return res;
  930. }
  931. /*
  932. * follow_up - Find the mountpoint of path's vfsmount
  933. *
  934. * Given a path, find the mountpoint of its source file system.
  935. * Replace @path with the path of the mountpoint in the parent mount.
  936. * Up is towards /.
  937. *
  938. * Return 1 if we went up a level and 0 if we were already at the
  939. * root.
  940. */
  941. int follow_up(struct path *path)
  942. {
  943. struct mount *mnt = real_mount(path->mnt);
  944. struct mount *parent;
  945. struct dentry *mountpoint;
  946. read_seqlock_excl(&mount_lock);
  947. parent = mnt->mnt_parent;
  948. if (parent == mnt) {
  949. read_sequnlock_excl(&mount_lock);
  950. return 0;
  951. }
  952. mntget(&parent->mnt);
  953. mountpoint = dget(mnt->mnt_mountpoint);
  954. read_sequnlock_excl(&mount_lock);
  955. dput(path->dentry);
  956. path->dentry = mountpoint;
  957. mntput(path->mnt);
  958. path->mnt = &parent->mnt;
  959. return 1;
  960. }
  961. EXPORT_SYMBOL(follow_up);
  962. /*
  963. * Perform an automount
  964. * - return -EISDIR to tell follow_managed() to stop and return the path we
  965. * were called with.
  966. */
  967. static int follow_automount(struct path *path, struct nameidata *nd,
  968. bool *need_mntput)
  969. {
  970. struct vfsmount *mnt;
  971. int err;
  972. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  973. return -EREMOTE;
  974. /* We don't want to mount if someone's just doing a stat -
  975. * unless they're stat'ing a directory and appended a '/' to
  976. * the name.
  977. *
  978. * We do, however, want to mount if someone wants to open or
  979. * create a file of any type under the mountpoint, wants to
  980. * traverse through the mountpoint or wants to open the
  981. * mounted directory. Also, autofs may mark negative dentries
  982. * as being automount points. These will need the attentions
  983. * of the daemon to instantiate them before they can be used.
  984. */
  985. if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  986. LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
  987. path->dentry->d_inode)
  988. return -EISDIR;
  989. nd->total_link_count++;
  990. if (nd->total_link_count >= 40)
  991. return -ELOOP;
  992. mnt = path->dentry->d_op->d_automount(path);
  993. if (IS_ERR(mnt)) {
  994. /*
  995. * The filesystem is allowed to return -EISDIR here to indicate
  996. * it doesn't want to automount. For instance, autofs would do
  997. * this so that its userspace daemon can mount on this dentry.
  998. *
  999. * However, we can only permit this if it's a terminal point in
  1000. * the path being looked up; if it wasn't then the remainder of
  1001. * the path is inaccessible and we should say so.
  1002. */
  1003. if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
  1004. return -EREMOTE;
  1005. return PTR_ERR(mnt);
  1006. }
  1007. if (!mnt) /* mount collision */
  1008. return 0;
  1009. if (!*need_mntput) {
  1010. /* lock_mount() may release path->mnt on error */
  1011. mntget(path->mnt);
  1012. *need_mntput = true;
  1013. }
  1014. err = finish_automount(mnt, path);
  1015. switch (err) {
  1016. case -EBUSY:
  1017. /* Someone else made a mount here whilst we were busy */
  1018. return 0;
  1019. case 0:
  1020. path_put(path);
  1021. path->mnt = mnt;
  1022. path->dentry = dget(mnt->mnt_root);
  1023. return 0;
  1024. default:
  1025. return err;
  1026. }
  1027. }
  1028. /*
  1029. * Handle a dentry that is managed in some way.
  1030. * - Flagged for transit management (autofs)
  1031. * - Flagged as mountpoint
  1032. * - Flagged as automount point
  1033. *
  1034. * This may only be called in refwalk mode.
  1035. *
  1036. * Serialization is taken care of in namespace.c
  1037. */
  1038. static int follow_managed(struct path *path, struct nameidata *nd)
  1039. {
  1040. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  1041. unsigned managed;
  1042. bool need_mntput = false;
  1043. int ret = 0;
  1044. /* Given that we're not holding a lock here, we retain the value in a
  1045. * local variable for each dentry as we look at it so that we don't see
  1046. * the components of that value change under us */
  1047. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1048. managed &= DCACHE_MANAGED_DENTRY,
  1049. unlikely(managed != 0)) {
  1050. /* Allow the filesystem to manage the transit without i_mutex
  1051. * being held. */
  1052. if (managed & DCACHE_MANAGE_TRANSIT) {
  1053. BUG_ON(!path->dentry->d_op);
  1054. BUG_ON(!path->dentry->d_op->d_manage);
  1055. ret = path->dentry->d_op->d_manage(path->dentry, false);
  1056. if (ret < 0)
  1057. break;
  1058. }
  1059. /* Transit to a mounted filesystem. */
  1060. if (managed & DCACHE_MOUNTED) {
  1061. struct vfsmount *mounted = lookup_mnt(path);
  1062. if (mounted) {
  1063. dput(path->dentry);
  1064. if (need_mntput)
  1065. mntput(path->mnt);
  1066. path->mnt = mounted;
  1067. path->dentry = dget(mounted->mnt_root);
  1068. need_mntput = true;
  1069. continue;
  1070. }
  1071. /* Something is mounted on this dentry in another
  1072. * namespace and/or whatever was mounted there in this
  1073. * namespace got unmounted before lookup_mnt() could
  1074. * get it */
  1075. }
  1076. /* Handle an automount point */
  1077. if (managed & DCACHE_NEED_AUTOMOUNT) {
  1078. ret = follow_automount(path, nd, &need_mntput);
  1079. if (ret < 0)
  1080. break;
  1081. continue;
  1082. }
  1083. /* We didn't change the current path point */
  1084. break;
  1085. }
  1086. if (need_mntput && path->mnt == mnt)
  1087. mntput(path->mnt);
  1088. if (ret == -EISDIR)
  1089. ret = 0;
  1090. if (need_mntput)
  1091. nd->flags |= LOOKUP_JUMPED;
  1092. if (unlikely(ret < 0))
  1093. path_put_conditional(path, nd);
  1094. return ret;
  1095. }
  1096. int follow_down_one(struct path *path)
  1097. {
  1098. struct vfsmount *mounted;
  1099. mounted = lookup_mnt(path);
  1100. if (mounted) {
  1101. dput(path->dentry);
  1102. mntput(path->mnt);
  1103. path->mnt = mounted;
  1104. path->dentry = dget(mounted->mnt_root);
  1105. return 1;
  1106. }
  1107. return 0;
  1108. }
  1109. EXPORT_SYMBOL(follow_down_one);
  1110. static inline int managed_dentry_rcu(struct dentry *dentry)
  1111. {
  1112. return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
  1113. dentry->d_op->d_manage(dentry, true) : 0;
  1114. }
  1115. /*
  1116. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  1117. * we meet a managed dentry that would need blocking.
  1118. */
  1119. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  1120. struct inode **inode, unsigned *seqp)
  1121. {
  1122. for (;;) {
  1123. struct mount *mounted;
  1124. /*
  1125. * Don't forget we might have a non-mountpoint managed dentry
  1126. * that wants to block transit.
  1127. */
  1128. switch (managed_dentry_rcu(path->dentry)) {
  1129. case -ECHILD:
  1130. default:
  1131. return false;
  1132. case -EISDIR:
  1133. return true;
  1134. case 0:
  1135. break;
  1136. }
  1137. if (!d_mountpoint(path->dentry))
  1138. return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1139. mounted = __lookup_mnt(path->mnt, path->dentry);
  1140. if (!mounted)
  1141. break;
  1142. path->mnt = &mounted->mnt;
  1143. path->dentry = mounted->mnt.mnt_root;
  1144. nd->flags |= LOOKUP_JUMPED;
  1145. *seqp = read_seqcount_begin(&path->dentry->d_seq);
  1146. /*
  1147. * Update the inode too. We don't need to re-check the
  1148. * dentry sequence number here after this d_inode read,
  1149. * because a mount-point is always pinned.
  1150. */
  1151. *inode = path->dentry->d_inode;
  1152. }
  1153. return !read_seqretry(&mount_lock, nd->m_seq) &&
  1154. !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1155. }
  1156. static int follow_dotdot_rcu(struct nameidata *nd)
  1157. {
  1158. struct inode *inode = nd->inode;
  1159. if (!nd->root.mnt)
  1160. set_root_rcu(nd);
  1161. while (1) {
  1162. if (path_equal(&nd->path, &nd->root))
  1163. break;
  1164. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1165. struct dentry *old = nd->path.dentry;
  1166. struct dentry *parent = old->d_parent;
  1167. unsigned seq;
  1168. inode = parent->d_inode;
  1169. seq = read_seqcount_begin(&parent->d_seq);
  1170. if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
  1171. return -ECHILD;
  1172. nd->path.dentry = parent;
  1173. nd->seq = seq;
  1174. if (unlikely(!path_connected(&nd->path)))
  1175. return -ENOENT;
  1176. break;
  1177. } else {
  1178. struct mount *mnt = real_mount(nd->path.mnt);
  1179. struct mount *mparent = mnt->mnt_parent;
  1180. struct dentry *mountpoint = mnt->mnt_mountpoint;
  1181. struct inode *inode2 = mountpoint->d_inode;
  1182. unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
  1183. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1184. return -ECHILD;
  1185. if (&mparent->mnt == nd->path.mnt)
  1186. break;
  1187. /* we know that mountpoint was pinned */
  1188. nd->path.dentry = mountpoint;
  1189. nd->path.mnt = &mparent->mnt;
  1190. inode = inode2;
  1191. nd->seq = seq;
  1192. }
  1193. }
  1194. while (unlikely(d_mountpoint(nd->path.dentry))) {
  1195. struct mount *mounted;
  1196. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
  1197. if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
  1198. return -ECHILD;
  1199. if (!mounted)
  1200. break;
  1201. nd->path.mnt = &mounted->mnt;
  1202. nd->path.dentry = mounted->mnt.mnt_root;
  1203. inode = nd->path.dentry->d_inode;
  1204. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1205. }
  1206. nd->inode = inode;
  1207. return 0;
  1208. }
  1209. /*
  1210. * Follow down to the covering mount currently visible to userspace. At each
  1211. * point, the filesystem owning that dentry may be queried as to whether the
  1212. * caller is permitted to proceed or not.
  1213. */
  1214. int follow_down(struct path *path)
  1215. {
  1216. unsigned managed;
  1217. int ret;
  1218. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1219. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  1220. /* Allow the filesystem to manage the transit without i_mutex
  1221. * being held.
  1222. *
  1223. * We indicate to the filesystem if someone is trying to mount
  1224. * something here. This gives autofs the chance to deny anyone
  1225. * other than its daemon the right to mount on its
  1226. * superstructure.
  1227. *
  1228. * The filesystem may sleep at this point.
  1229. */
  1230. if (managed & DCACHE_MANAGE_TRANSIT) {
  1231. BUG_ON(!path->dentry->d_op);
  1232. BUG_ON(!path->dentry->d_op->d_manage);
  1233. ret = path->dentry->d_op->d_manage(
  1234. path->dentry, false);
  1235. if (ret < 0)
  1236. return ret == -EISDIR ? 0 : ret;
  1237. }
  1238. /* Transit to a mounted filesystem. */
  1239. if (managed & DCACHE_MOUNTED) {
  1240. struct vfsmount *mounted = lookup_mnt(path);
  1241. if (!mounted)
  1242. break;
  1243. dput(path->dentry);
  1244. mntput(path->mnt);
  1245. path->mnt = mounted;
  1246. path->dentry = dget(mounted->mnt_root);
  1247. continue;
  1248. }
  1249. /* Don't handle automount points here */
  1250. break;
  1251. }
  1252. return 0;
  1253. }
  1254. EXPORT_SYMBOL(follow_down);
  1255. /*
  1256. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1257. */
  1258. static void follow_mount(struct path *path)
  1259. {
  1260. while (d_mountpoint(path->dentry)) {
  1261. struct vfsmount *mounted = lookup_mnt(path);
  1262. if (!mounted)
  1263. break;
  1264. dput(path->dentry);
  1265. mntput(path->mnt);
  1266. path->mnt = mounted;
  1267. path->dentry = dget(mounted->mnt_root);
  1268. }
  1269. }
  1270. static int follow_dotdot(struct nameidata *nd)
  1271. {
  1272. if (!nd->root.mnt)
  1273. set_root(nd);
  1274. while(1) {
  1275. struct dentry *old = nd->path.dentry;
  1276. if (nd->path.dentry == nd->root.dentry &&
  1277. nd->path.mnt == nd->root.mnt) {
  1278. break;
  1279. }
  1280. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1281. /* rare case of legitimate dget_parent()... */
  1282. nd->path.dentry = dget_parent(nd->path.dentry);
  1283. dput(old);
  1284. if (unlikely(!path_connected(&nd->path)))
  1285. return -ENOENT;
  1286. break;
  1287. }
  1288. if (!follow_up(&nd->path))
  1289. break;
  1290. }
  1291. follow_mount(&nd->path);
  1292. nd->inode = nd->path.dentry->d_inode;
  1293. return 0;
  1294. }
  1295. /*
  1296. * This looks up the name in dcache, possibly revalidates the old dentry and
  1297. * allocates a new one if not found or not valid. In the need_lookup argument
  1298. * returns whether i_op->lookup is necessary.
  1299. *
  1300. * dir->d_inode->i_mutex must be held
  1301. */
  1302. static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
  1303. unsigned int flags, bool *need_lookup)
  1304. {
  1305. struct dentry *dentry;
  1306. int error;
  1307. *need_lookup = false;
  1308. dentry = d_lookup(dir, name);
  1309. if (dentry) {
  1310. if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
  1311. error = d_revalidate(dentry, flags);
  1312. if (unlikely(error <= 0)) {
  1313. if (error < 0) {
  1314. dput(dentry);
  1315. return ERR_PTR(error);
  1316. } else {
  1317. d_invalidate(dentry);
  1318. dput(dentry);
  1319. dentry = NULL;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. if (!dentry) {
  1325. dentry = d_alloc(dir, name);
  1326. if (unlikely(!dentry))
  1327. return ERR_PTR(-ENOMEM);
  1328. *need_lookup = true;
  1329. }
  1330. return dentry;
  1331. }
  1332. /*
  1333. * Call i_op->lookup on the dentry. The dentry must be negative and
  1334. * unhashed.
  1335. *
  1336. * dir->d_inode->i_mutex must be held
  1337. */
  1338. static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
  1339. unsigned int flags)
  1340. {
  1341. struct dentry *old;
  1342. /* Don't create child dentry for a dead directory. */
  1343. if (unlikely(IS_DEADDIR(dir))) {
  1344. dput(dentry);
  1345. return ERR_PTR(-ENOENT);
  1346. }
  1347. old = dir->i_op->lookup(dir, dentry, flags);
  1348. if (unlikely(old)) {
  1349. dput(dentry);
  1350. dentry = old;
  1351. }
  1352. return dentry;
  1353. }
  1354. static struct dentry *__lookup_hash(struct qstr *name,
  1355. struct dentry *base, unsigned int flags)
  1356. {
  1357. bool need_lookup;
  1358. struct dentry *dentry;
  1359. dentry = lookup_dcache(name, base, flags, &need_lookup);
  1360. if (!need_lookup)
  1361. return dentry;
  1362. return lookup_real(base->d_inode, dentry, flags);
  1363. }
  1364. /*
  1365. * It's more convoluted than I'd like it to be, but... it's still fairly
  1366. * small and for now I'd prefer to have fast path as straight as possible.
  1367. * It _is_ time-critical.
  1368. */
  1369. static int lookup_fast(struct nameidata *nd,
  1370. struct path *path, struct inode **inode,
  1371. unsigned *seqp)
  1372. {
  1373. struct vfsmount *mnt = nd->path.mnt;
  1374. struct dentry *dentry, *parent = nd->path.dentry;
  1375. int need_reval = 1;
  1376. int status = 1;
  1377. int err;
  1378. /*
  1379. * Rename seqlock is not required here because in the off chance
  1380. * of a false negative due to a concurrent rename, we're going to
  1381. * do the non-racy lookup, below.
  1382. */
  1383. if (nd->flags & LOOKUP_RCU) {
  1384. unsigned seq;
  1385. bool negative;
  1386. dentry = __d_lookup_rcu(parent, &nd->last, &seq);
  1387. if (!dentry)
  1388. goto unlazy;
  1389. /*
  1390. * This sequence count validates that the inode matches
  1391. * the dentry name information from lookup.
  1392. */
  1393. *inode = d_backing_inode(dentry);
  1394. negative = d_is_negative(dentry);
  1395. if (read_seqcount_retry(&dentry->d_seq, seq))
  1396. return -ECHILD;
  1397. /*
  1398. * This sequence count validates that the parent had no
  1399. * changes while we did the lookup of the dentry above.
  1400. *
  1401. * The memory barrier in read_seqcount_begin of child is
  1402. * enough, we can use __read_seqcount_retry here.
  1403. */
  1404. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1405. return -ECHILD;
  1406. *seqp = seq;
  1407. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1408. status = d_revalidate(dentry, nd->flags);
  1409. if (unlikely(status <= 0)) {
  1410. if (status != -ECHILD)
  1411. need_reval = 0;
  1412. goto unlazy;
  1413. }
  1414. }
  1415. /*
  1416. * Note: do negative dentry check after revalidation in
  1417. * case that drops it.
  1418. */
  1419. if (negative)
  1420. return -ENOENT;
  1421. path->mnt = mnt;
  1422. path->dentry = dentry;
  1423. if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
  1424. return 0;
  1425. unlazy:
  1426. if (unlazy_walk(nd, dentry, seq))
  1427. return -ECHILD;
  1428. } else {
  1429. dentry = __d_lookup(parent, &nd->last);
  1430. }
  1431. if (unlikely(!dentry))
  1432. goto need_lookup;
  1433. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1434. status = d_revalidate(dentry, nd->flags);
  1435. if (unlikely(status <= 0)) {
  1436. if (status < 0) {
  1437. dput(dentry);
  1438. return status;
  1439. }
  1440. d_invalidate(dentry);
  1441. dput(dentry);
  1442. goto need_lookup;
  1443. }
  1444. if (unlikely(d_is_negative(dentry))) {
  1445. dput(dentry);
  1446. return -ENOENT;
  1447. }
  1448. path->mnt = mnt;
  1449. path->dentry = dentry;
  1450. err = follow_managed(path, nd);
  1451. if (likely(!err))
  1452. *inode = d_backing_inode(path->dentry);
  1453. return err;
  1454. need_lookup:
  1455. return 1;
  1456. }
  1457. /* Fast lookup failed, do it the slow way */
  1458. static int lookup_slow(struct nameidata *nd, struct path *path)
  1459. {
  1460. struct dentry *dentry, *parent;
  1461. parent = nd->path.dentry;
  1462. BUG_ON(nd->inode != parent->d_inode);
  1463. mutex_lock(&parent->d_inode->i_mutex);
  1464. dentry = __lookup_hash(&nd->last, parent, nd->flags);
  1465. mutex_unlock(&parent->d_inode->i_mutex);
  1466. if (IS_ERR(dentry))
  1467. return PTR_ERR(dentry);
  1468. path->mnt = nd->path.mnt;
  1469. path->dentry = dentry;
  1470. return follow_managed(path, nd);
  1471. }
  1472. static inline int may_lookup(struct nameidata *nd)
  1473. {
  1474. if (nd->flags & LOOKUP_RCU) {
  1475. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1476. if (err != -ECHILD)
  1477. return err;
  1478. if (unlazy_walk(nd, NULL, 0))
  1479. return -ECHILD;
  1480. }
  1481. return inode_permission(nd->inode, MAY_EXEC);
  1482. }
  1483. static inline int handle_dots(struct nameidata *nd, int type)
  1484. {
  1485. if (type == LAST_DOTDOT) {
  1486. if (nd->flags & LOOKUP_RCU) {
  1487. return follow_dotdot_rcu(nd);
  1488. } else
  1489. return follow_dotdot(nd);
  1490. }
  1491. return 0;
  1492. }
  1493. static int pick_link(struct nameidata *nd, struct path *link,
  1494. struct inode *inode, unsigned seq)
  1495. {
  1496. int error;
  1497. struct saved *last;
  1498. if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
  1499. path_to_nameidata(link, nd);
  1500. return -ELOOP;
  1501. }
  1502. if (!(nd->flags & LOOKUP_RCU)) {
  1503. if (link->mnt == nd->path.mnt)
  1504. mntget(link->mnt);
  1505. }
  1506. error = nd_alloc_stack(nd);
  1507. if (unlikely(error)) {
  1508. if (error == -ECHILD) {
  1509. if (unlikely(unlazy_link(nd, link, seq)))
  1510. return -ECHILD;
  1511. error = nd_alloc_stack(nd);
  1512. }
  1513. if (error) {
  1514. path_put(link);
  1515. return error;
  1516. }
  1517. }
  1518. last = nd->stack + nd->depth++;
  1519. last->link = *link;
  1520. last->cookie = NULL;
  1521. last->inode = inode;
  1522. last->seq = seq;
  1523. return 1;
  1524. }
  1525. /*
  1526. * Do we need to follow links? We _really_ want to be able
  1527. * to do this check without having to look at inode->i_op,
  1528. * so we keep a cache of "no, this doesn't need follow_link"
  1529. * for the common case.
  1530. */
  1531. static inline int should_follow_link(struct nameidata *nd, struct path *link,
  1532. int follow,
  1533. struct inode *inode, unsigned seq)
  1534. {
  1535. if (likely(!d_is_symlink(link->dentry)))
  1536. return 0;
  1537. if (!follow)
  1538. return 0;
  1539. return pick_link(nd, link, inode, seq);
  1540. }
  1541. enum {WALK_GET = 1, WALK_PUT = 2};
  1542. static int walk_component(struct nameidata *nd, int flags)
  1543. {
  1544. struct path path;
  1545. struct inode *inode;
  1546. unsigned seq;
  1547. int err;
  1548. /*
  1549. * "." and ".." are special - ".." especially so because it has
  1550. * to be able to know about the current root directory and
  1551. * parent relationships.
  1552. */
  1553. if (unlikely(nd->last_type != LAST_NORM)) {
  1554. err = handle_dots(nd, nd->last_type);
  1555. if (flags & WALK_PUT)
  1556. put_link(nd);
  1557. return err;
  1558. }
  1559. err = lookup_fast(nd, &path, &inode, &seq);
  1560. if (unlikely(err)) {
  1561. if (err < 0)
  1562. return err;
  1563. err = lookup_slow(nd, &path);
  1564. if (err < 0)
  1565. return err;
  1566. inode = d_backing_inode(path.dentry);
  1567. seq = 0; /* we are already out of RCU mode */
  1568. err = -ENOENT;
  1569. if (d_is_negative(path.dentry))
  1570. goto out_path_put;
  1571. }
  1572. if (flags & WALK_PUT)
  1573. put_link(nd);
  1574. err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
  1575. if (unlikely(err))
  1576. return err;
  1577. path_to_nameidata(&path, nd);
  1578. nd->inode = inode;
  1579. nd->seq = seq;
  1580. return 0;
  1581. out_path_put:
  1582. path_to_nameidata(&path, nd);
  1583. return err;
  1584. }
  1585. /*
  1586. * We can do the critical dentry name comparison and hashing
  1587. * operations one word at a time, but we are limited to:
  1588. *
  1589. * - Architectures with fast unaligned word accesses. We could
  1590. * do a "get_unaligned()" if this helps and is sufficiently
  1591. * fast.
  1592. *
  1593. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1594. * do not trap on the (extremely unlikely) case of a page
  1595. * crossing operation.
  1596. *
  1597. * - Furthermore, we need an efficient 64-bit compile for the
  1598. * 64-bit case in order to generate the "number of bytes in
  1599. * the final mask". Again, that could be replaced with a
  1600. * efficient population count instruction or similar.
  1601. */
  1602. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1603. #include <asm/word-at-a-time.h>
  1604. #ifdef CONFIG_64BIT
  1605. static inline unsigned int fold_hash(unsigned long hash)
  1606. {
  1607. return hash_64(hash, 32);
  1608. }
  1609. #else /* 32-bit case */
  1610. #define fold_hash(x) (x)
  1611. #endif
  1612. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1613. {
  1614. unsigned long a, mask;
  1615. unsigned long hash = 0;
  1616. for (;;) {
  1617. a = load_unaligned_zeropad(name);
  1618. if (len < sizeof(unsigned long))
  1619. break;
  1620. hash += a;
  1621. hash *= 9;
  1622. name += sizeof(unsigned long);
  1623. len -= sizeof(unsigned long);
  1624. if (!len)
  1625. goto done;
  1626. }
  1627. mask = bytemask_from_count(len);
  1628. hash += mask & a;
  1629. done:
  1630. return fold_hash(hash);
  1631. }
  1632. EXPORT_SYMBOL(full_name_hash);
  1633. /*
  1634. * Calculate the length and hash of the path component, and
  1635. * return the "hash_len" as the result.
  1636. */
  1637. static inline u64 hash_name(const char *name)
  1638. {
  1639. unsigned long a, b, adata, bdata, mask, hash, len;
  1640. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1641. hash = a = 0;
  1642. len = -sizeof(unsigned long);
  1643. do {
  1644. hash = (hash + a) * 9;
  1645. len += sizeof(unsigned long);
  1646. a = load_unaligned_zeropad(name+len);
  1647. b = a ^ REPEAT_BYTE('/');
  1648. } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
  1649. adata = prep_zero_mask(a, adata, &constants);
  1650. bdata = prep_zero_mask(b, bdata, &constants);
  1651. mask = create_zero_mask(adata | bdata);
  1652. hash += a & zero_bytemask(mask);
  1653. len += find_zero(mask);
  1654. return hashlen_create(fold_hash(hash), len);
  1655. }
  1656. #else
  1657. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1658. {
  1659. unsigned long hash = init_name_hash();
  1660. while (len--)
  1661. hash = partial_name_hash(*name++, hash);
  1662. return end_name_hash(hash);
  1663. }
  1664. EXPORT_SYMBOL(full_name_hash);
  1665. /*
  1666. * We know there's a real path component here of at least
  1667. * one character.
  1668. */
  1669. static inline u64 hash_name(const char *name)
  1670. {
  1671. unsigned long hash = init_name_hash();
  1672. unsigned long len = 0, c;
  1673. c = (unsigned char)*name;
  1674. do {
  1675. len++;
  1676. hash = partial_name_hash(c, hash);
  1677. c = (unsigned char)name[len];
  1678. } while (c && c != '/');
  1679. return hashlen_create(end_name_hash(hash), len);
  1680. }
  1681. #endif
  1682. /*
  1683. * Name resolution.
  1684. * This is the basic name resolution function, turning a pathname into
  1685. * the final dentry. We expect 'base' to be positive and a directory.
  1686. *
  1687. * Returns 0 and nd will have valid dentry and mnt on success.
  1688. * Returns error and drops reference to input namei data on failure.
  1689. */
  1690. static int link_path_walk(const char *name, struct nameidata *nd)
  1691. {
  1692. int err;
  1693. while (*name=='/')
  1694. name++;
  1695. if (!*name)
  1696. return 0;
  1697. /* At this point we know we have a real path component. */
  1698. for(;;) {
  1699. u64 hash_len;
  1700. int type;
  1701. err = may_lookup(nd);
  1702. if (err)
  1703. return err;
  1704. hash_len = hash_name(name);
  1705. type = LAST_NORM;
  1706. if (name[0] == '.') switch (hashlen_len(hash_len)) {
  1707. case 2:
  1708. if (name[1] == '.') {
  1709. type = LAST_DOTDOT;
  1710. nd->flags |= LOOKUP_JUMPED;
  1711. }
  1712. break;
  1713. case 1:
  1714. type = LAST_DOT;
  1715. }
  1716. if (likely(type == LAST_NORM)) {
  1717. struct dentry *parent = nd->path.dentry;
  1718. nd->flags &= ~LOOKUP_JUMPED;
  1719. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1720. struct qstr this = { { .hash_len = hash_len }, .name = name };
  1721. err = parent->d_op->d_hash(parent, &this);
  1722. if (err < 0)
  1723. return err;
  1724. hash_len = this.hash_len;
  1725. name = this.name;
  1726. }
  1727. }
  1728. nd->last.hash_len = hash_len;
  1729. nd->last.name = name;
  1730. nd->last_type = type;
  1731. name += hashlen_len(hash_len);
  1732. if (!*name)
  1733. goto OK;
  1734. /*
  1735. * If it wasn't NUL, we know it was '/'. Skip that
  1736. * slash, and continue until no more slashes.
  1737. */
  1738. do {
  1739. name++;
  1740. } while (unlikely(*name == '/'));
  1741. if (unlikely(!*name)) {
  1742. OK:
  1743. /* pathname body, done */
  1744. if (!nd->depth)
  1745. return 0;
  1746. name = nd->stack[nd->depth - 1].name;
  1747. /* trailing symlink, done */
  1748. if (!name)
  1749. return 0;
  1750. /* last component of nested symlink */
  1751. err = walk_component(nd, WALK_GET | WALK_PUT);
  1752. } else {
  1753. err = walk_component(nd, WALK_GET);
  1754. }
  1755. if (err < 0)
  1756. return err;
  1757. if (err) {
  1758. const char *s = get_link(nd);
  1759. if (unlikely(IS_ERR(s)))
  1760. return PTR_ERR(s);
  1761. err = 0;
  1762. if (unlikely(!s)) {
  1763. /* jumped */
  1764. put_link(nd);
  1765. } else {
  1766. nd->stack[nd->depth - 1].name = name;
  1767. name = s;
  1768. continue;
  1769. }
  1770. }
  1771. if (unlikely(!d_can_lookup(nd->path.dentry))) {
  1772. if (nd->flags & LOOKUP_RCU) {
  1773. if (unlazy_walk(nd, NULL, 0))
  1774. return -ECHILD;
  1775. }
  1776. return -ENOTDIR;
  1777. }
  1778. }
  1779. }
  1780. static const char *path_init(struct nameidata *nd, unsigned flags)
  1781. {
  1782. int retval = 0;
  1783. const char *s = nd->name->name;
  1784. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1785. nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
  1786. nd->depth = 0;
  1787. nd->total_link_count = 0;
  1788. if (flags & LOOKUP_ROOT) {
  1789. struct dentry *root = nd->root.dentry;
  1790. struct inode *inode = root->d_inode;
  1791. if (*s) {
  1792. if (!d_can_lookup(root))
  1793. return ERR_PTR(-ENOTDIR);
  1794. retval = inode_permission(inode, MAY_EXEC);
  1795. if (retval)
  1796. return ERR_PTR(retval);
  1797. }
  1798. nd->path = nd->root;
  1799. nd->inode = inode;
  1800. if (flags & LOOKUP_RCU) {
  1801. rcu_read_lock();
  1802. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1803. nd->root_seq = nd->seq;
  1804. nd->m_seq = read_seqbegin(&mount_lock);
  1805. } else {
  1806. path_get(&nd->path);
  1807. }
  1808. return s;
  1809. }
  1810. nd->root.mnt = NULL;
  1811. nd->m_seq = read_seqbegin(&mount_lock);
  1812. if (*s == '/') {
  1813. if (flags & LOOKUP_RCU) {
  1814. rcu_read_lock();
  1815. set_root_rcu(nd);
  1816. nd->seq = nd->root_seq;
  1817. } else {
  1818. set_root(nd);
  1819. path_get(&nd->root);
  1820. }
  1821. nd->path = nd->root;
  1822. } else if (nd->dfd == AT_FDCWD) {
  1823. if (flags & LOOKUP_RCU) {
  1824. struct fs_struct *fs = current->fs;
  1825. unsigned seq;
  1826. rcu_read_lock();
  1827. do {
  1828. seq = read_seqcount_begin(&fs->seq);
  1829. nd->path = fs->pwd;
  1830. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1831. } while (read_seqcount_retry(&fs->seq, seq));
  1832. } else {
  1833. get_fs_pwd(current->fs, &nd->path);
  1834. }
  1835. } else {
  1836. /* Caller must check execute permissions on the starting path component */
  1837. struct fd f = fdget_raw(nd->dfd);
  1838. struct dentry *dentry;
  1839. if (!f.file)
  1840. return ERR_PTR(-EBADF);
  1841. dentry = f.file->f_path.dentry;
  1842. if (*s) {
  1843. if (!d_can_lookup(dentry)) {
  1844. fdput(f);
  1845. return ERR_PTR(-ENOTDIR);
  1846. }
  1847. }
  1848. nd->path = f.file->f_path;
  1849. if (flags & LOOKUP_RCU) {
  1850. rcu_read_lock();
  1851. nd->inode = nd->path.dentry->d_inode;
  1852. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1853. } else {
  1854. path_get(&nd->path);
  1855. nd->inode = nd->path.dentry->d_inode;
  1856. }
  1857. fdput(f);
  1858. return s;
  1859. }
  1860. nd->inode = nd->path.dentry->d_inode;
  1861. if (!(flags & LOOKUP_RCU))
  1862. return s;
  1863. if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
  1864. return s;
  1865. if (!(nd->flags & LOOKUP_ROOT))
  1866. nd->root.mnt = NULL;
  1867. rcu_read_unlock();
  1868. return ERR_PTR(-ECHILD);
  1869. }
  1870. static const char *trailing_symlink(struct nameidata *nd)
  1871. {
  1872. const char *s;
  1873. int error = may_follow_link(nd);
  1874. if (unlikely(error))
  1875. return ERR_PTR(error);
  1876. nd->flags |= LOOKUP_PARENT;
  1877. nd->stack[0].name = NULL;
  1878. s = get_link(nd);
  1879. return s ? s : "";
  1880. }
  1881. static inline int lookup_last(struct nameidata *nd)
  1882. {
  1883. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1884. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1885. nd->flags &= ~LOOKUP_PARENT;
  1886. return walk_component(nd,
  1887. nd->flags & LOOKUP_FOLLOW
  1888. ? nd->depth
  1889. ? WALK_PUT | WALK_GET
  1890. : WALK_GET
  1891. : 0);
  1892. }
  1893. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1894. static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
  1895. {
  1896. const char *s = path_init(nd, flags);
  1897. int err;
  1898. if (IS_ERR(s))
  1899. return PTR_ERR(s);
  1900. while (!(err = link_path_walk(s, nd))
  1901. && ((err = lookup_last(nd)) > 0)) {
  1902. s = trailing_symlink(nd);
  1903. if (IS_ERR(s)) {
  1904. err = PTR_ERR(s);
  1905. break;
  1906. }
  1907. }
  1908. if (!err)
  1909. err = complete_walk(nd);
  1910. if (!err && nd->flags & LOOKUP_DIRECTORY)
  1911. if (!d_can_lookup(nd->path.dentry))
  1912. err = -ENOTDIR;
  1913. if (!err) {
  1914. *path = nd->path;
  1915. nd->path.mnt = NULL;
  1916. nd->path.dentry = NULL;
  1917. }
  1918. terminate_walk(nd);
  1919. return err;
  1920. }
  1921. static int filename_lookup(int dfd, struct filename *name, unsigned flags,
  1922. struct path *path, struct path *root)
  1923. {
  1924. int retval;
  1925. struct nameidata nd;
  1926. if (IS_ERR(name))
  1927. return PTR_ERR(name);
  1928. if (unlikely(root)) {
  1929. nd.root = *root;
  1930. flags |= LOOKUP_ROOT;
  1931. }
  1932. set_nameidata(&nd, dfd, name);
  1933. retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
  1934. if (unlikely(retval == -ECHILD))
  1935. retval = path_lookupat(&nd, flags, path);
  1936. if (unlikely(retval == -ESTALE))
  1937. retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
  1938. if (likely(!retval))
  1939. audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
  1940. restore_nameidata();
  1941. putname(name);
  1942. return retval;
  1943. }
  1944. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1945. static int path_parentat(struct nameidata *nd, unsigned flags,
  1946. struct path *parent)
  1947. {
  1948. const char *s = path_init(nd, flags);
  1949. int err;
  1950. if (IS_ERR(s))
  1951. return PTR_ERR(s);
  1952. err = link_path_walk(s, nd);
  1953. if (!err)
  1954. err = complete_walk(nd);
  1955. if (!err) {
  1956. *parent = nd->path;
  1957. nd->path.mnt = NULL;
  1958. nd->path.dentry = NULL;
  1959. }
  1960. terminate_walk(nd);
  1961. return err;
  1962. }
  1963. static struct filename *filename_parentat(int dfd, struct filename *name,
  1964. unsigned int flags, struct path *parent,
  1965. struct qstr *last, int *type)
  1966. {
  1967. int retval;
  1968. struct nameidata nd;
  1969. if (IS_ERR(name))
  1970. return name;
  1971. set_nameidata(&nd, dfd, name);
  1972. retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
  1973. if (unlikely(retval == -ECHILD))
  1974. retval = path_parentat(&nd, flags, parent);
  1975. if (unlikely(retval == -ESTALE))
  1976. retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
  1977. if (likely(!retval)) {
  1978. *last = nd.last;
  1979. *type = nd.last_type;
  1980. audit_inode(name, parent->dentry, LOOKUP_PARENT);
  1981. } else {
  1982. putname(name);
  1983. name = ERR_PTR(retval);
  1984. }
  1985. restore_nameidata();
  1986. return name;
  1987. }
  1988. /* does lookup, returns the object with parent locked */
  1989. struct dentry *kern_path_locked(const char *name, struct path *path)
  1990. {
  1991. struct filename *filename;
  1992. struct dentry *d;
  1993. struct qstr last;
  1994. int type;
  1995. filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
  1996. &last, &type);
  1997. if (IS_ERR(filename))
  1998. return ERR_CAST(filename);
  1999. if (unlikely(type != LAST_NORM)) {
  2000. path_put(path);
  2001. putname(filename);
  2002. return ERR_PTR(-EINVAL);
  2003. }
  2004. mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2005. d = __lookup_hash(&last, path->dentry, 0);
  2006. if (IS_ERR(d)) {
  2007. mutex_unlock(&path->dentry->d_inode->i_mutex);
  2008. path_put(path);
  2009. }
  2010. putname(filename);
  2011. return d;
  2012. }
  2013. int kern_path(const char *name, unsigned int flags, struct path *path)
  2014. {
  2015. return filename_lookup(AT_FDCWD, getname_kernel(name),
  2016. flags, path, NULL);
  2017. }
  2018. EXPORT_SYMBOL(kern_path);
  2019. /**
  2020. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  2021. * @dentry: pointer to dentry of the base directory
  2022. * @mnt: pointer to vfs mount of the base directory
  2023. * @name: pointer to file name
  2024. * @flags: lookup flags
  2025. * @path: pointer to struct path to fill
  2026. */
  2027. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  2028. const char *name, unsigned int flags,
  2029. struct path *path)
  2030. {
  2031. struct path root = {.mnt = mnt, .dentry = dentry};
  2032. /* the first argument of filename_lookup() is ignored with root */
  2033. return filename_lookup(AT_FDCWD, getname_kernel(name),
  2034. flags , path, &root);
  2035. }
  2036. EXPORT_SYMBOL(vfs_path_lookup);
  2037. /**
  2038. * lookup_one_len - filesystem helper to lookup single pathname component
  2039. * @name: pathname component to lookup
  2040. * @base: base directory to lookup from
  2041. * @len: maximum length @len should be interpreted to
  2042. *
  2043. * Note that this routine is purely a helper for filesystem usage and should
  2044. * not be called by generic code.
  2045. */
  2046. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  2047. {
  2048. struct qstr this;
  2049. unsigned int c;
  2050. int err;
  2051. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  2052. this.name = name;
  2053. this.len = len;
  2054. this.hash = full_name_hash(name, len);
  2055. if (!len)
  2056. return ERR_PTR(-EACCES);
  2057. if (unlikely(name[0] == '.')) {
  2058. if (len < 2 || (len == 2 && name[1] == '.'))
  2059. return ERR_PTR(-EACCES);
  2060. }
  2061. while (len--) {
  2062. c = *(const unsigned char *)name++;
  2063. if (c == '/' || c == '\0')
  2064. return ERR_PTR(-EACCES);
  2065. }
  2066. /*
  2067. * See if the low-level filesystem might want
  2068. * to use its own hash..
  2069. */
  2070. if (base->d_flags & DCACHE_OP_HASH) {
  2071. int err = base->d_op->d_hash(base, &this);
  2072. if (err < 0)
  2073. return ERR_PTR(err);
  2074. }
  2075. err = inode_permission(base->d_inode, MAY_EXEC);
  2076. if (err)
  2077. return ERR_PTR(err);
  2078. return __lookup_hash(&this, base, 0);
  2079. }
  2080. EXPORT_SYMBOL(lookup_one_len);
  2081. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  2082. struct path *path, int *empty)
  2083. {
  2084. return filename_lookup(dfd, getname_flags(name, flags, empty),
  2085. flags, path, NULL);
  2086. }
  2087. EXPORT_SYMBOL(user_path_at_empty);
  2088. /*
  2089. * NB: most callers don't do anything directly with the reference to the
  2090. * to struct filename, but the nd->last pointer points into the name string
  2091. * allocated by getname. So we must hold the reference to it until all
  2092. * path-walking is complete.
  2093. */
  2094. static inline struct filename *
  2095. user_path_parent(int dfd, const char __user *path,
  2096. struct path *parent,
  2097. struct qstr *last,
  2098. int *type,
  2099. unsigned int flags)
  2100. {
  2101. /* only LOOKUP_REVAL is allowed in extra flags */
  2102. return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
  2103. parent, last, type);
  2104. }
  2105. /**
  2106. * mountpoint_last - look up last component for umount
  2107. * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
  2108. * @path: pointer to container for result
  2109. *
  2110. * This is a special lookup_last function just for umount. In this case, we
  2111. * need to resolve the path without doing any revalidation.
  2112. *
  2113. * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
  2114. * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
  2115. * in almost all cases, this lookup will be served out of the dcache. The only
  2116. * cases where it won't are if nd->last refers to a symlink or the path is
  2117. * bogus and it doesn't exist.
  2118. *
  2119. * Returns:
  2120. * -error: if there was an error during lookup. This includes -ENOENT if the
  2121. * lookup found a negative dentry. The nd->path reference will also be
  2122. * put in this case.
  2123. *
  2124. * 0: if we successfully resolved nd->path and found it to not to be a
  2125. * symlink that needs to be followed. "path" will also be populated.
  2126. * The nd->path reference will also be put.
  2127. *
  2128. * 1: if we successfully resolved nd->last and found it to be a symlink
  2129. * that needs to be followed. "path" will be populated with the path
  2130. * to the link, and nd->path will *not* be put.
  2131. */
  2132. static int
  2133. mountpoint_last(struct nameidata *nd, struct path *path)
  2134. {
  2135. int error = 0;
  2136. struct dentry *dentry;
  2137. struct dentry *dir = nd->path.dentry;
  2138. /* If we're in rcuwalk, drop out of it to handle last component */
  2139. if (nd->flags & LOOKUP_RCU) {
  2140. if (unlazy_walk(nd, NULL, 0))
  2141. return -ECHILD;
  2142. }
  2143. nd->flags &= ~LOOKUP_PARENT;
  2144. if (unlikely(nd->last_type != LAST_NORM)) {
  2145. error = handle_dots(nd, nd->last_type);
  2146. if (error)
  2147. return error;
  2148. dentry = dget(nd->path.dentry);
  2149. goto done;
  2150. }
  2151. mutex_lock(&dir->d_inode->i_mutex);
  2152. dentry = d_lookup(dir, &nd->last);
  2153. if (!dentry) {
  2154. /*
  2155. * No cached dentry. Mounted dentries are pinned in the cache,
  2156. * so that means that this dentry is probably a symlink or the
  2157. * path doesn't actually point to a mounted dentry.
  2158. */
  2159. dentry = d_alloc(dir, &nd->last);
  2160. if (!dentry) {
  2161. mutex_unlock(&dir->d_inode->i_mutex);
  2162. return -ENOMEM;
  2163. }
  2164. dentry = lookup_real(dir->d_inode, dentry, nd->flags);
  2165. if (IS_ERR(dentry)) {
  2166. mutex_unlock(&dir->d_inode->i_mutex);
  2167. return PTR_ERR(dentry);
  2168. }
  2169. }
  2170. mutex_unlock(&dir->d_inode->i_mutex);
  2171. done:
  2172. if (d_is_negative(dentry)) {
  2173. dput(dentry);
  2174. return -ENOENT;
  2175. }
  2176. if (nd->depth)
  2177. put_link(nd);
  2178. path->dentry = dentry;
  2179. path->mnt = nd->path.mnt;
  2180. error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
  2181. d_backing_inode(dentry), 0);
  2182. if (unlikely(error))
  2183. return error;
  2184. mntget(path->mnt);
  2185. follow_mount(path);
  2186. return 0;
  2187. }
  2188. /**
  2189. * path_mountpoint - look up a path to be umounted
  2190. * @nd: lookup context
  2191. * @flags: lookup flags
  2192. * @path: pointer to container for result
  2193. *
  2194. * Look up the given name, but don't attempt to revalidate the last component.
  2195. * Returns 0 and "path" will be valid on success; Returns error otherwise.
  2196. */
  2197. static int
  2198. path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
  2199. {
  2200. const char *s = path_init(nd, flags);
  2201. int err;
  2202. if (IS_ERR(s))
  2203. return PTR_ERR(s);
  2204. while (!(err = link_path_walk(s, nd)) &&
  2205. (err = mountpoint_last(nd, path)) > 0) {
  2206. s = trailing_symlink(nd);
  2207. if (IS_ERR(s)) {
  2208. err = PTR_ERR(s);
  2209. break;
  2210. }
  2211. }
  2212. terminate_walk(nd);
  2213. return err;
  2214. }
  2215. static int
  2216. filename_mountpoint(int dfd, struct filename *name, struct path *path,
  2217. unsigned int flags)
  2218. {
  2219. struct nameidata nd;
  2220. int error;
  2221. if (IS_ERR(name))
  2222. return PTR_ERR(name);
  2223. set_nameidata(&nd, dfd, name);
  2224. error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
  2225. if (unlikely(error == -ECHILD))
  2226. error = path_mountpoint(&nd, flags, path);
  2227. if (unlikely(error == -ESTALE))
  2228. error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
  2229. if (likely(!error))
  2230. audit_inode(name, path->dentry, 0);
  2231. restore_nameidata();
  2232. putname(name);
  2233. return error;
  2234. }
  2235. /**
  2236. * user_path_mountpoint_at - lookup a path from userland in order to umount it
  2237. * @dfd: directory file descriptor
  2238. * @name: pathname from userland
  2239. * @flags: lookup flags
  2240. * @path: pointer to container to hold result
  2241. *
  2242. * A umount is a special case for path walking. We're not actually interested
  2243. * in the inode in this situation, and ESTALE errors can be a problem. We
  2244. * simply want track down the dentry and vfsmount attached at the mountpoint
  2245. * and avoid revalidating the last component.
  2246. *
  2247. * Returns 0 and populates "path" on success.
  2248. */
  2249. int
  2250. user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
  2251. struct path *path)
  2252. {
  2253. return filename_mountpoint(dfd, getname(name), path, flags);
  2254. }
  2255. int
  2256. kern_path_mountpoint(int dfd, const char *name, struct path *path,
  2257. unsigned int flags)
  2258. {
  2259. return filename_mountpoint(dfd, getname_kernel(name), path, flags);
  2260. }
  2261. EXPORT_SYMBOL(kern_path_mountpoint);
  2262. int __check_sticky(struct inode *dir, struct inode *inode)
  2263. {
  2264. kuid_t fsuid = current_fsuid();
  2265. if (uid_eq(inode->i_uid, fsuid))
  2266. return 0;
  2267. if (uid_eq(dir->i_uid, fsuid))
  2268. return 0;
  2269. return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
  2270. }
  2271. EXPORT_SYMBOL(__check_sticky);
  2272. /*
  2273. * Check whether we can remove a link victim from directory dir, check
  2274. * whether the type of victim is right.
  2275. * 1. We can't do it if dir is read-only (done in permission())
  2276. * 2. We should have write and exec permissions on dir
  2277. * 3. We can't remove anything from append-only dir
  2278. * 4. We can't do anything with immutable dir (done in permission())
  2279. * 5. If the sticky bit on dir is set we should either
  2280. * a. be owner of dir, or
  2281. * b. be owner of victim, or
  2282. * c. have CAP_FOWNER capability
  2283. * 6. If the victim is append-only or immutable we can't do antyhing with
  2284. * links pointing to it.
  2285. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  2286. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  2287. * 9. We can't remove a root or mountpoint.
  2288. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  2289. * nfs_async_unlink().
  2290. */
  2291. static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
  2292. {
  2293. struct inode *inode = d_backing_inode(victim);
  2294. int error;
  2295. if (d_is_negative(victim))
  2296. return -ENOENT;
  2297. BUG_ON(!inode);
  2298. BUG_ON(victim->d_parent->d_inode != dir);
  2299. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  2300. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2301. if (error)
  2302. return error;
  2303. if (IS_APPEND(dir))
  2304. return -EPERM;
  2305. if (check_sticky(dir, inode) || IS_APPEND(inode) ||
  2306. IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
  2307. return -EPERM;
  2308. if (isdir) {
  2309. if (!d_is_dir(victim))
  2310. return -ENOTDIR;
  2311. if (IS_ROOT(victim))
  2312. return -EBUSY;
  2313. } else if (d_is_dir(victim))
  2314. return -EISDIR;
  2315. if (IS_DEADDIR(dir))
  2316. return -ENOENT;
  2317. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  2318. return -EBUSY;
  2319. return 0;
  2320. }
  2321. /* Check whether we can create an object with dentry child in directory
  2322. * dir.
  2323. * 1. We can't do it if child already exists (open has special treatment for
  2324. * this case, but since we are inlined it's OK)
  2325. * 2. We can't do it if dir is read-only (done in permission())
  2326. * 3. We should have write and exec permissions on dir
  2327. * 4. We can't do it if dir is immutable (done in permission())
  2328. */
  2329. static inline int may_create(struct inode *dir, struct dentry *child)
  2330. {
  2331. audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
  2332. if (child->d_inode)
  2333. return -EEXIST;
  2334. if (IS_DEADDIR(dir))
  2335. return -ENOENT;
  2336. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2337. }
  2338. /*
  2339. * p1 and p2 should be directories on the same fs.
  2340. */
  2341. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  2342. {
  2343. struct dentry *p;
  2344. if (p1 == p2) {
  2345. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  2346. return NULL;
  2347. }
  2348. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  2349. p = d_ancestor(p2, p1);
  2350. if (p) {
  2351. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  2352. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  2353. return p;
  2354. }
  2355. p = d_ancestor(p1, p2);
  2356. if (p) {
  2357. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  2358. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  2359. return p;
  2360. }
  2361. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  2362. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
  2363. return NULL;
  2364. }
  2365. EXPORT_SYMBOL(lock_rename);
  2366. void unlock_rename(struct dentry *p1, struct dentry *p2)
  2367. {
  2368. mutex_unlock(&p1->d_inode->i_mutex);
  2369. if (p1 != p2) {
  2370. mutex_unlock(&p2->d_inode->i_mutex);
  2371. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  2372. }
  2373. }
  2374. EXPORT_SYMBOL(unlock_rename);
  2375. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2376. bool want_excl)
  2377. {
  2378. int error = may_create(dir, dentry);
  2379. if (error)
  2380. return error;
  2381. if (!dir->i_op->create)
  2382. return -EACCES; /* shouldn't it be ENOSYS? */
  2383. mode &= S_IALLUGO;
  2384. mode |= S_IFREG;
  2385. error = security_inode_create(dir, dentry, mode);
  2386. if (error)
  2387. return error;
  2388. error = dir->i_op->create(dir, dentry, mode, want_excl);
  2389. if (!error)
  2390. fsnotify_create(dir, dentry);
  2391. return error;
  2392. }
  2393. EXPORT_SYMBOL(vfs_create);
  2394. static int may_open(struct path *path, int acc_mode, int flag)
  2395. {
  2396. struct dentry *dentry = path->dentry;
  2397. struct inode *inode = dentry->d_inode;
  2398. int error;
  2399. /* O_PATH? */
  2400. if (!acc_mode)
  2401. return 0;
  2402. if (!inode)
  2403. return -ENOENT;
  2404. switch (inode->i_mode & S_IFMT) {
  2405. case S_IFLNK:
  2406. return -ELOOP;
  2407. case S_IFDIR:
  2408. if (acc_mode & MAY_WRITE)
  2409. return -EISDIR;
  2410. break;
  2411. case S_IFBLK:
  2412. case S_IFCHR:
  2413. if (path->mnt->mnt_flags & MNT_NODEV)
  2414. return -EACCES;
  2415. /*FALLTHRU*/
  2416. case S_IFIFO:
  2417. case S_IFSOCK:
  2418. flag &= ~O_TRUNC;
  2419. break;
  2420. }
  2421. error = inode_permission(inode, acc_mode);
  2422. if (error)
  2423. return error;
  2424. /*
  2425. * An append-only file must be opened in append mode for writing.
  2426. */
  2427. if (IS_APPEND(inode)) {
  2428. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  2429. return -EPERM;
  2430. if (flag & O_TRUNC)
  2431. return -EPERM;
  2432. }
  2433. /* O_NOATIME can only be set by the owner or superuser */
  2434. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  2435. return -EPERM;
  2436. return 0;
  2437. }
  2438. static int handle_truncate(struct file *filp)
  2439. {
  2440. struct path *path = &filp->f_path;
  2441. struct inode *inode = path->dentry->d_inode;
  2442. int error = get_write_access(inode);
  2443. if (error)
  2444. return error;
  2445. /*
  2446. * Refuse to truncate files with mandatory locks held on them.
  2447. */
  2448. error = locks_verify_locked(filp);
  2449. if (!error)
  2450. error = security_path_truncate(path);
  2451. if (!error) {
  2452. error = do_truncate(path->dentry, 0,
  2453. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  2454. filp);
  2455. }
  2456. put_write_access(inode);
  2457. return error;
  2458. }
  2459. static inline int open_to_namei_flags(int flag)
  2460. {
  2461. if ((flag & O_ACCMODE) == 3)
  2462. flag--;
  2463. return flag;
  2464. }
  2465. static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
  2466. {
  2467. int error = security_path_mknod(dir, dentry, mode, 0);
  2468. if (error)
  2469. return error;
  2470. error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
  2471. if (error)
  2472. return error;
  2473. return security_inode_create(dir->dentry->d_inode, dentry, mode);
  2474. }
  2475. /*
  2476. * Attempt to atomically look up, create and open a file from a negative
  2477. * dentry.
  2478. *
  2479. * Returns 0 if successful. The file will have been created and attached to
  2480. * @file by the filesystem calling finish_open().
  2481. *
  2482. * Returns 1 if the file was looked up only or didn't need creating. The
  2483. * caller will need to perform the open themselves. @path will have been
  2484. * updated to point to the new dentry. This may be negative.
  2485. *
  2486. * Returns an error code otherwise.
  2487. */
  2488. static int atomic_open(struct nameidata *nd, struct dentry *dentry,
  2489. struct path *path, struct file *file,
  2490. const struct open_flags *op,
  2491. bool got_write, bool need_lookup,
  2492. int *opened)
  2493. {
  2494. struct inode *dir = nd->path.dentry->d_inode;
  2495. unsigned open_flag = open_to_namei_flags(op->open_flag);
  2496. umode_t mode;
  2497. int error;
  2498. int acc_mode;
  2499. int create_error = 0;
  2500. struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
  2501. bool excl;
  2502. BUG_ON(dentry->d_inode);
  2503. /* Don't create child dentry for a dead directory. */
  2504. if (unlikely(IS_DEADDIR(dir))) {
  2505. error = -ENOENT;
  2506. goto out;
  2507. }
  2508. mode = op->mode;
  2509. if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
  2510. mode &= ~current_umask();
  2511. excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
  2512. if (excl)
  2513. open_flag &= ~O_TRUNC;
  2514. /*
  2515. * Checking write permission is tricky, bacuse we don't know if we are
  2516. * going to actually need it: O_CREAT opens should work as long as the
  2517. * file exists. But checking existence breaks atomicity. The trick is
  2518. * to check access and if not granted clear O_CREAT from the flags.
  2519. *
  2520. * Another problem is returing the "right" error value (e.g. for an
  2521. * O_EXCL open we want to return EEXIST not EROFS).
  2522. */
  2523. if (((open_flag & (O_CREAT | O_TRUNC)) ||
  2524. (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
  2525. if (!(open_flag & O_CREAT)) {
  2526. /*
  2527. * No O_CREATE -> atomicity not a requirement -> fall
  2528. * back to lookup + open
  2529. */
  2530. goto no_open;
  2531. } else if (open_flag & (O_EXCL | O_TRUNC)) {
  2532. /* Fall back and fail with the right error */
  2533. create_error = -EROFS;
  2534. goto no_open;
  2535. } else {
  2536. /* No side effects, safe to clear O_CREAT */
  2537. create_error = -EROFS;
  2538. open_flag &= ~O_CREAT;
  2539. }
  2540. }
  2541. if (open_flag & O_CREAT) {
  2542. error = may_o_create(&nd->path, dentry, mode);
  2543. if (error) {
  2544. create_error = error;
  2545. if (open_flag & O_EXCL)
  2546. goto no_open;
  2547. open_flag &= ~O_CREAT;
  2548. }
  2549. }
  2550. if (nd->flags & LOOKUP_DIRECTORY)
  2551. open_flag |= O_DIRECTORY;
  2552. file->f_path.dentry = DENTRY_NOT_SET;
  2553. file->f_path.mnt = nd->path.mnt;
  2554. error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
  2555. opened);
  2556. if (error < 0) {
  2557. if (create_error && error == -ENOENT)
  2558. error = create_error;
  2559. goto out;
  2560. }
  2561. if (error) { /* returned 1, that is */
  2562. if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
  2563. error = -EIO;
  2564. goto out;
  2565. }
  2566. if (file->f_path.dentry) {
  2567. dput(dentry);
  2568. dentry = file->f_path.dentry;
  2569. }
  2570. if (*opened & FILE_CREATED)
  2571. fsnotify_create(dir, dentry);
  2572. if (!dentry->d_inode) {
  2573. WARN_ON(*opened & FILE_CREATED);
  2574. if (create_error) {
  2575. error = create_error;
  2576. goto out;
  2577. }
  2578. } else {
  2579. if (excl && !(*opened & FILE_CREATED)) {
  2580. error = -EEXIST;
  2581. goto out;
  2582. }
  2583. }
  2584. goto looked_up;
  2585. }
  2586. /*
  2587. * We didn't have the inode before the open, so check open permission
  2588. * here.
  2589. */
  2590. acc_mode = op->acc_mode;
  2591. if (*opened & FILE_CREATED) {
  2592. WARN_ON(!(open_flag & O_CREAT));
  2593. fsnotify_create(dir, dentry);
  2594. acc_mode = MAY_OPEN;
  2595. }
  2596. error = may_open(&file->f_path, acc_mode, open_flag);
  2597. if (error)
  2598. fput(file);
  2599. out:
  2600. dput(dentry);
  2601. return error;
  2602. no_open:
  2603. if (need_lookup) {
  2604. dentry = lookup_real(dir, dentry, nd->flags);
  2605. if (IS_ERR(dentry))
  2606. return PTR_ERR(dentry);
  2607. if (create_error) {
  2608. int open_flag = op->open_flag;
  2609. error = create_error;
  2610. if ((open_flag & O_EXCL)) {
  2611. if (!dentry->d_inode)
  2612. goto out;
  2613. } else if (!dentry->d_inode) {
  2614. goto out;
  2615. } else if ((open_flag & O_TRUNC) &&
  2616. d_is_reg(dentry)) {
  2617. goto out;
  2618. }
  2619. /* will fail later, go on to get the right error */
  2620. }
  2621. }
  2622. looked_up:
  2623. path->dentry = dentry;
  2624. path->mnt = nd->path.mnt;
  2625. return 1;
  2626. }
  2627. /*
  2628. * Look up and maybe create and open the last component.
  2629. *
  2630. * Must be called with i_mutex held on parent.
  2631. *
  2632. * Returns 0 if the file was successfully atomically created (if necessary) and
  2633. * opened. In this case the file will be returned attached to @file.
  2634. *
  2635. * Returns 1 if the file was not completely opened at this time, though lookups
  2636. * and creations will have been performed and the dentry returned in @path will
  2637. * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
  2638. * specified then a negative dentry may be returned.
  2639. *
  2640. * An error code is returned otherwise.
  2641. *
  2642. * FILE_CREATE will be set in @*opened if the dentry was created and will be
  2643. * cleared otherwise prior to returning.
  2644. */
  2645. static int lookup_open(struct nameidata *nd, struct path *path,
  2646. struct file *file,
  2647. const struct open_flags *op,
  2648. bool got_write, int *opened)
  2649. {
  2650. struct dentry *dir = nd->path.dentry;
  2651. struct inode *dir_inode = dir->d_inode;
  2652. struct dentry *dentry;
  2653. int error;
  2654. bool need_lookup;
  2655. *opened &= ~FILE_CREATED;
  2656. dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
  2657. if (IS_ERR(dentry))
  2658. return PTR_ERR(dentry);
  2659. /* Cached positive dentry: will open in f_op->open */
  2660. if (!need_lookup && dentry->d_inode)
  2661. goto out_no_open;
  2662. if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
  2663. return atomic_open(nd, dentry, path, file, op, got_write,
  2664. need_lookup, opened);
  2665. }
  2666. if (need_lookup) {
  2667. BUG_ON(dentry->d_inode);
  2668. dentry = lookup_real(dir_inode, dentry, nd->flags);
  2669. if (IS_ERR(dentry))
  2670. return PTR_ERR(dentry);
  2671. }
  2672. /* Negative dentry, just create the file */
  2673. if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
  2674. umode_t mode = op->mode;
  2675. if (!IS_POSIXACL(dir->d_inode))
  2676. mode &= ~current_umask();
  2677. /*
  2678. * This write is needed to ensure that a
  2679. * rw->ro transition does not occur between
  2680. * the time when the file is created and when
  2681. * a permanent write count is taken through
  2682. * the 'struct file' in finish_open().
  2683. */
  2684. if (!got_write) {
  2685. error = -EROFS;
  2686. goto out_dput;
  2687. }
  2688. *opened |= FILE_CREATED;
  2689. error = security_path_mknod(&nd->path, dentry, mode, 0);
  2690. if (error)
  2691. goto out_dput;
  2692. error = vfs_create(dir->d_inode, dentry, mode,
  2693. nd->flags & LOOKUP_EXCL);
  2694. if (error)
  2695. goto out_dput;
  2696. }
  2697. out_no_open:
  2698. path->dentry = dentry;
  2699. path->mnt = nd->path.mnt;
  2700. return 1;
  2701. out_dput:
  2702. dput(dentry);
  2703. return error;
  2704. }
  2705. /*
  2706. * Handle the last step of open()
  2707. */
  2708. static int do_last(struct nameidata *nd,
  2709. struct file *file, const struct open_flags *op,
  2710. int *opened)
  2711. {
  2712. struct dentry *dir = nd->path.dentry;
  2713. int open_flag = op->open_flag;
  2714. bool will_truncate = (open_flag & O_TRUNC) != 0;
  2715. bool got_write = false;
  2716. int acc_mode = op->acc_mode;
  2717. unsigned seq;
  2718. struct inode *inode;
  2719. struct path save_parent = { .dentry = NULL, .mnt = NULL };
  2720. struct path path;
  2721. bool retried = false;
  2722. int error;
  2723. nd->flags &= ~LOOKUP_PARENT;
  2724. nd->flags |= op->intent;
  2725. if (nd->last_type != LAST_NORM) {
  2726. error = handle_dots(nd, nd->last_type);
  2727. if (unlikely(error))
  2728. return error;
  2729. goto finish_open;
  2730. }
  2731. if (!(open_flag & O_CREAT)) {
  2732. if (nd->last.name[nd->last.len])
  2733. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2734. /* we _can_ be in RCU mode here */
  2735. error = lookup_fast(nd, &path, &inode, &seq);
  2736. if (likely(!error))
  2737. goto finish_lookup;
  2738. if (error < 0)
  2739. return error;
  2740. BUG_ON(nd->inode != dir->d_inode);
  2741. } else {
  2742. /* create side of things */
  2743. /*
  2744. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
  2745. * has been cleared when we got to the last component we are
  2746. * about to look up
  2747. */
  2748. error = complete_walk(nd);
  2749. if (error)
  2750. return error;
  2751. audit_inode(nd->name, dir, LOOKUP_PARENT);
  2752. /* trailing slashes? */
  2753. if (unlikely(nd->last.name[nd->last.len]))
  2754. return -EISDIR;
  2755. }
  2756. retry_lookup:
  2757. if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
  2758. error = mnt_want_write(nd->path.mnt);
  2759. if (!error)
  2760. got_write = true;
  2761. /*
  2762. * do _not_ fail yet - we might not need that or fail with
  2763. * a different error; let lookup_open() decide; we'll be
  2764. * dropping this one anyway.
  2765. */
  2766. }
  2767. mutex_lock(&dir->d_inode->i_mutex);
  2768. error = lookup_open(nd, &path, file, op, got_write, opened);
  2769. mutex_unlock(&dir->d_inode->i_mutex);
  2770. if (error <= 0) {
  2771. if (error)
  2772. goto out;
  2773. if ((*opened & FILE_CREATED) ||
  2774. !S_ISREG(file_inode(file)->i_mode))
  2775. will_truncate = false;
  2776. audit_inode(nd->name, file->f_path.dentry, 0);
  2777. goto opened;
  2778. }
  2779. if (*opened & FILE_CREATED) {
  2780. /* Don't check for write permission, don't truncate */
  2781. open_flag &= ~O_TRUNC;
  2782. will_truncate = false;
  2783. acc_mode = MAY_OPEN;
  2784. path_to_nameidata(&path, nd);
  2785. goto finish_open_created;
  2786. }
  2787. /*
  2788. * create/update audit record if it already exists.
  2789. */
  2790. if (d_is_positive(path.dentry))
  2791. audit_inode(nd->name, path.dentry, 0);
  2792. /*
  2793. * If atomic_open() acquired write access it is dropped now due to
  2794. * possible mount and symlink following (this might be optimized away if
  2795. * necessary...)
  2796. */
  2797. if (got_write) {
  2798. mnt_drop_write(nd->path.mnt);
  2799. got_write = false;
  2800. }
  2801. if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
  2802. path_to_nameidata(&path, nd);
  2803. return -EEXIST;
  2804. }
  2805. error = follow_managed(&path, nd);
  2806. if (unlikely(error < 0))
  2807. return error;
  2808. BUG_ON(nd->flags & LOOKUP_RCU);
  2809. inode = d_backing_inode(path.dentry);
  2810. seq = 0; /* out of RCU mode, so the value doesn't matter */
  2811. if (unlikely(d_is_negative(path.dentry))) {
  2812. path_to_nameidata(&path, nd);
  2813. return -ENOENT;
  2814. }
  2815. finish_lookup:
  2816. if (nd->depth)
  2817. put_link(nd);
  2818. error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
  2819. inode, seq);
  2820. if (unlikely(error))
  2821. return error;
  2822. if (unlikely(d_is_symlink(path.dentry)) && !(open_flag & O_PATH)) {
  2823. path_to_nameidata(&path, nd);
  2824. return -ELOOP;
  2825. }
  2826. if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path.mnt) {
  2827. path_to_nameidata(&path, nd);
  2828. } else {
  2829. save_parent.dentry = nd->path.dentry;
  2830. save_parent.mnt = mntget(path.mnt);
  2831. nd->path.dentry = path.dentry;
  2832. }
  2833. nd->inode = inode;
  2834. nd->seq = seq;
  2835. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2836. finish_open:
  2837. error = complete_walk(nd);
  2838. if (error) {
  2839. path_put(&save_parent);
  2840. return error;
  2841. }
  2842. audit_inode(nd->name, nd->path.dentry, 0);
  2843. error = -EISDIR;
  2844. if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
  2845. goto out;
  2846. error = -ENOTDIR;
  2847. if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
  2848. goto out;
  2849. if (!d_is_reg(nd->path.dentry))
  2850. will_truncate = false;
  2851. if (will_truncate) {
  2852. error = mnt_want_write(nd->path.mnt);
  2853. if (error)
  2854. goto out;
  2855. got_write = true;
  2856. }
  2857. finish_open_created:
  2858. error = may_open(&nd->path, acc_mode, open_flag);
  2859. if (error)
  2860. goto out;
  2861. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  2862. error = vfs_open(&nd->path, file, current_cred());
  2863. if (!error) {
  2864. *opened |= FILE_OPENED;
  2865. } else {
  2866. if (error == -EOPENSTALE)
  2867. goto stale_open;
  2868. goto out;
  2869. }
  2870. opened:
  2871. error = open_check_o_direct(file);
  2872. if (error)
  2873. goto exit_fput;
  2874. error = ima_file_check(file, op->acc_mode, *opened);
  2875. if (error)
  2876. goto exit_fput;
  2877. if (will_truncate) {
  2878. error = handle_truncate(file);
  2879. if (error)
  2880. goto exit_fput;
  2881. }
  2882. out:
  2883. if (got_write)
  2884. mnt_drop_write(nd->path.mnt);
  2885. path_put(&save_parent);
  2886. return error;
  2887. exit_fput:
  2888. fput(file);
  2889. goto out;
  2890. stale_open:
  2891. /* If no saved parent or already retried then can't retry */
  2892. if (!save_parent.dentry || retried)
  2893. goto out;
  2894. BUG_ON(save_parent.dentry != dir);
  2895. path_put(&nd->path);
  2896. nd->path = save_parent;
  2897. nd->inode = dir->d_inode;
  2898. save_parent.mnt = NULL;
  2899. save_parent.dentry = NULL;
  2900. if (got_write) {
  2901. mnt_drop_write(nd->path.mnt);
  2902. got_write = false;
  2903. }
  2904. retried = true;
  2905. goto retry_lookup;
  2906. }
  2907. static int do_tmpfile(struct nameidata *nd, unsigned flags,
  2908. const struct open_flags *op,
  2909. struct file *file, int *opened)
  2910. {
  2911. static const struct qstr name = QSTR_INIT("/", 1);
  2912. struct dentry *child;
  2913. struct inode *dir;
  2914. struct path path;
  2915. int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
  2916. if (unlikely(error))
  2917. return error;
  2918. error = mnt_want_write(path.mnt);
  2919. if (unlikely(error))
  2920. goto out;
  2921. dir = path.dentry->d_inode;
  2922. /* we want directory to be writable */
  2923. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2924. if (error)
  2925. goto out2;
  2926. if (!dir->i_op->tmpfile) {
  2927. error = -EOPNOTSUPP;
  2928. goto out2;
  2929. }
  2930. child = d_alloc(path.dentry, &name);
  2931. if (unlikely(!child)) {
  2932. error = -ENOMEM;
  2933. goto out2;
  2934. }
  2935. dput(path.dentry);
  2936. path.dentry = child;
  2937. error = dir->i_op->tmpfile(dir, child, op->mode);
  2938. if (error)
  2939. goto out2;
  2940. audit_inode(nd->name, child, 0);
  2941. /* Don't check for other permissions, the inode was just created */
  2942. error = may_open(&path, MAY_OPEN, op->open_flag);
  2943. if (error)
  2944. goto out2;
  2945. file->f_path.mnt = path.mnt;
  2946. error = finish_open(file, child, NULL, opened);
  2947. if (error)
  2948. goto out2;
  2949. error = open_check_o_direct(file);
  2950. if (error) {
  2951. fput(file);
  2952. } else if (!(op->open_flag & O_EXCL)) {
  2953. struct inode *inode = file_inode(file);
  2954. spin_lock(&inode->i_lock);
  2955. inode->i_state |= I_LINKABLE;
  2956. spin_unlock(&inode->i_lock);
  2957. }
  2958. out2:
  2959. mnt_drop_write(path.mnt);
  2960. out:
  2961. path_put(&path);
  2962. return error;
  2963. }
  2964. static struct file *path_openat(struct nameidata *nd,
  2965. const struct open_flags *op, unsigned flags)
  2966. {
  2967. const char *s;
  2968. struct file *file;
  2969. int opened = 0;
  2970. int error;
  2971. file = get_empty_filp();
  2972. if (IS_ERR(file))
  2973. return file;
  2974. file->f_flags = op->open_flag;
  2975. if (unlikely(file->f_flags & __O_TMPFILE)) {
  2976. error = do_tmpfile(nd, flags, op, file, &opened);
  2977. goto out2;
  2978. }
  2979. s = path_init(nd, flags);
  2980. if (IS_ERR(s)) {
  2981. put_filp(file);
  2982. return ERR_CAST(s);
  2983. }
  2984. while (!(error = link_path_walk(s, nd)) &&
  2985. (error = do_last(nd, file, op, &opened)) > 0) {
  2986. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2987. s = trailing_symlink(nd);
  2988. if (IS_ERR(s)) {
  2989. error = PTR_ERR(s);
  2990. break;
  2991. }
  2992. }
  2993. terminate_walk(nd);
  2994. out2:
  2995. if (!(opened & FILE_OPENED)) {
  2996. BUG_ON(!error);
  2997. put_filp(file);
  2998. }
  2999. if (unlikely(error)) {
  3000. if (error == -EOPENSTALE) {
  3001. if (flags & LOOKUP_RCU)
  3002. error = -ECHILD;
  3003. else
  3004. error = -ESTALE;
  3005. }
  3006. file = ERR_PTR(error);
  3007. }
  3008. return file;
  3009. }
  3010. struct file *do_filp_open(int dfd, struct filename *pathname,
  3011. const struct open_flags *op)
  3012. {
  3013. struct nameidata nd;
  3014. int flags = op->lookup_flags;
  3015. struct file *filp;
  3016. set_nameidata(&nd, dfd, pathname);
  3017. filp = path_openat(&nd, op, flags | LOOKUP_RCU);
  3018. if (unlikely(filp == ERR_PTR(-ECHILD)))
  3019. filp = path_openat(&nd, op, flags);
  3020. if (unlikely(filp == ERR_PTR(-ESTALE)))
  3021. filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
  3022. restore_nameidata();
  3023. return filp;
  3024. }
  3025. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  3026. const char *name, const struct open_flags *op)
  3027. {
  3028. struct nameidata nd;
  3029. struct file *file;
  3030. struct filename *filename;
  3031. int flags = op->lookup_flags | LOOKUP_ROOT;
  3032. nd.root.mnt = mnt;
  3033. nd.root.dentry = dentry;
  3034. if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
  3035. return ERR_PTR(-ELOOP);
  3036. filename = getname_kernel(name);
  3037. if (unlikely(IS_ERR(filename)))
  3038. return ERR_CAST(filename);
  3039. set_nameidata(&nd, -1, filename);
  3040. file = path_openat(&nd, op, flags | LOOKUP_RCU);
  3041. if (unlikely(file == ERR_PTR(-ECHILD)))
  3042. file = path_openat(&nd, op, flags);
  3043. if (unlikely(file == ERR_PTR(-ESTALE)))
  3044. file = path_openat(&nd, op, flags | LOOKUP_REVAL);
  3045. restore_nameidata();
  3046. putname(filename);
  3047. return file;
  3048. }
  3049. static struct dentry *filename_create(int dfd, struct filename *name,
  3050. struct path *path, unsigned int lookup_flags)
  3051. {
  3052. struct dentry *dentry = ERR_PTR(-EEXIST);
  3053. struct qstr last;
  3054. int type;
  3055. int err2;
  3056. int error;
  3057. bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
  3058. /*
  3059. * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
  3060. * other flags passed in are ignored!
  3061. */
  3062. lookup_flags &= LOOKUP_REVAL;
  3063. name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
  3064. if (IS_ERR(name))
  3065. return ERR_CAST(name);
  3066. /*
  3067. * Yucky last component or no last component at all?
  3068. * (foo/., foo/.., /////)
  3069. */
  3070. if (unlikely(type != LAST_NORM))
  3071. goto out;
  3072. /* don't fail immediately if it's r/o, at least try to report other errors */
  3073. err2 = mnt_want_write(path->mnt);
  3074. /*
  3075. * Do the final lookup.
  3076. */
  3077. lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  3078. mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3079. dentry = __lookup_hash(&last, path->dentry, lookup_flags);
  3080. if (IS_ERR(dentry))
  3081. goto unlock;
  3082. error = -EEXIST;
  3083. if (d_is_positive(dentry))
  3084. goto fail;
  3085. /*
  3086. * Special case - lookup gave negative, but... we had foo/bar/
  3087. * From the vfs_mknod() POV we just have a negative dentry -
  3088. * all is fine. Let's be bastards - you had / on the end, you've
  3089. * been asking for (non-existent) directory. -ENOENT for you.
  3090. */
  3091. if (unlikely(!is_dir && last.name[last.len])) {
  3092. error = -ENOENT;
  3093. goto fail;
  3094. }
  3095. if (unlikely(err2)) {
  3096. error = err2;
  3097. goto fail;
  3098. }
  3099. putname(name);
  3100. return dentry;
  3101. fail:
  3102. dput(dentry);
  3103. dentry = ERR_PTR(error);
  3104. unlock:
  3105. mutex_unlock(&path->dentry->d_inode->i_mutex);
  3106. if (!err2)
  3107. mnt_drop_write(path->mnt);
  3108. out:
  3109. path_put(path);
  3110. putname(name);
  3111. return dentry;
  3112. }
  3113. struct dentry *kern_path_create(int dfd, const char *pathname,
  3114. struct path *path, unsigned int lookup_flags)
  3115. {
  3116. return filename_create(dfd, getname_kernel(pathname),
  3117. path, lookup_flags);
  3118. }
  3119. EXPORT_SYMBOL(kern_path_create);
  3120. void done_path_create(struct path *path, struct dentry *dentry)
  3121. {
  3122. dput(dentry);
  3123. mutex_unlock(&path->dentry->d_inode->i_mutex);
  3124. mnt_drop_write(path->mnt);
  3125. path_put(path);
  3126. }
  3127. EXPORT_SYMBOL(done_path_create);
  3128. inline struct dentry *user_path_create(int dfd, const char __user *pathname,
  3129. struct path *path, unsigned int lookup_flags)
  3130. {
  3131. return filename_create(dfd, getname(pathname), path, lookup_flags);
  3132. }
  3133. EXPORT_SYMBOL(user_path_create);
  3134. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  3135. {
  3136. int error = may_create(dir, dentry);
  3137. if (error)
  3138. return error;
  3139. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  3140. return -EPERM;
  3141. if (!dir->i_op->mknod)
  3142. return -EPERM;
  3143. error = devcgroup_inode_mknod(mode, dev);
  3144. if (error)
  3145. return error;
  3146. error = security_inode_mknod(dir, dentry, mode, dev);
  3147. if (error)
  3148. return error;
  3149. error = dir->i_op->mknod(dir, dentry, mode, dev);
  3150. if (!error)
  3151. fsnotify_create(dir, dentry);
  3152. return error;
  3153. }
  3154. EXPORT_SYMBOL(vfs_mknod);
  3155. static int may_mknod(umode_t mode)
  3156. {
  3157. switch (mode & S_IFMT) {
  3158. case S_IFREG:
  3159. case S_IFCHR:
  3160. case S_IFBLK:
  3161. case S_IFIFO:
  3162. case S_IFSOCK:
  3163. case 0: /* zero mode translates to S_IFREG */
  3164. return 0;
  3165. case S_IFDIR:
  3166. return -EPERM;
  3167. default:
  3168. return -EINVAL;
  3169. }
  3170. }
  3171. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  3172. unsigned, dev)
  3173. {
  3174. struct dentry *dentry;
  3175. struct path path;
  3176. int error;
  3177. unsigned int lookup_flags = 0;
  3178. error = may_mknod(mode);
  3179. if (error)
  3180. return error;
  3181. retry:
  3182. dentry = user_path_create(dfd, filename, &path, lookup_flags);
  3183. if (IS_ERR(dentry))
  3184. return PTR_ERR(dentry);
  3185. if (!IS_POSIXACL(path.dentry->d_inode))
  3186. mode &= ~current_umask();
  3187. error = security_path_mknod(&path, dentry, mode, dev);
  3188. if (error)
  3189. goto out;
  3190. switch (mode & S_IFMT) {
  3191. case 0: case S_IFREG:
  3192. error = vfs_create(path.dentry->d_inode,dentry,mode,true);
  3193. break;
  3194. case S_IFCHR: case S_IFBLK:
  3195. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  3196. new_decode_dev(dev));
  3197. break;
  3198. case S_IFIFO: case S_IFSOCK:
  3199. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  3200. break;
  3201. }
  3202. out:
  3203. done_path_create(&path, dentry);
  3204. if (retry_estale(error, lookup_flags)) {
  3205. lookup_flags |= LOOKUP_REVAL;
  3206. goto retry;
  3207. }
  3208. return error;
  3209. }
  3210. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  3211. {
  3212. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  3213. }
  3214. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3215. {
  3216. int error = may_create(dir, dentry);
  3217. unsigned max_links = dir->i_sb->s_max_links;
  3218. if (error)
  3219. return error;
  3220. if (!dir->i_op->mkdir)
  3221. return -EPERM;
  3222. mode &= (S_IRWXUGO|S_ISVTX);
  3223. error = security_inode_mkdir(dir, dentry, mode);
  3224. if (error)
  3225. return error;
  3226. if (max_links && dir->i_nlink >= max_links)
  3227. return -EMLINK;
  3228. error = dir->i_op->mkdir(dir, dentry, mode);
  3229. if (!error)
  3230. fsnotify_mkdir(dir, dentry);
  3231. return error;
  3232. }
  3233. EXPORT_SYMBOL(vfs_mkdir);
  3234. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  3235. {
  3236. struct dentry *dentry;
  3237. struct path path;
  3238. int error;
  3239. unsigned int lookup_flags = LOOKUP_DIRECTORY;
  3240. retry:
  3241. dentry = user_path_create(dfd, pathname, &path, lookup_flags);
  3242. if (IS_ERR(dentry))
  3243. return PTR_ERR(dentry);
  3244. if (!IS_POSIXACL(path.dentry->d_inode))
  3245. mode &= ~current_umask();
  3246. error = security_path_mkdir(&path, dentry, mode);
  3247. if (!error)
  3248. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  3249. done_path_create(&path, dentry);
  3250. if (retry_estale(error, lookup_flags)) {
  3251. lookup_flags |= LOOKUP_REVAL;
  3252. goto retry;
  3253. }
  3254. return error;
  3255. }
  3256. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  3257. {
  3258. return sys_mkdirat(AT_FDCWD, pathname, mode);
  3259. }
  3260. /*
  3261. * The dentry_unhash() helper will try to drop the dentry early: we
  3262. * should have a usage count of 1 if we're the only user of this
  3263. * dentry, and if that is true (possibly after pruning the dcache),
  3264. * then we drop the dentry now.
  3265. *
  3266. * A low-level filesystem can, if it choses, legally
  3267. * do a
  3268. *
  3269. * if (!d_unhashed(dentry))
  3270. * return -EBUSY;
  3271. *
  3272. * if it cannot handle the case of removing a directory
  3273. * that is still in use by something else..
  3274. */
  3275. void dentry_unhash(struct dentry *dentry)
  3276. {
  3277. shrink_dcache_parent(dentry);
  3278. spin_lock(&dentry->d_lock);
  3279. if (dentry->d_lockref.count == 1)
  3280. __d_drop(dentry);
  3281. spin_unlock(&dentry->d_lock);
  3282. }
  3283. EXPORT_SYMBOL(dentry_unhash);
  3284. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  3285. {
  3286. int error = may_delete(dir, dentry, 1);
  3287. if (error)
  3288. return error;
  3289. if (!dir->i_op->rmdir)
  3290. return -EPERM;
  3291. dget(dentry);
  3292. mutex_lock(&dentry->d_inode->i_mutex);
  3293. error = -EBUSY;
  3294. if (is_local_mountpoint(dentry))
  3295. goto out;
  3296. error = security_inode_rmdir(dir, dentry);
  3297. if (error)
  3298. goto out;
  3299. shrink_dcache_parent(dentry);
  3300. error = dir->i_op->rmdir(dir, dentry);
  3301. if (error)
  3302. goto out;
  3303. dentry->d_inode->i_flags |= S_DEAD;
  3304. dont_mount(dentry);
  3305. detach_mounts(dentry);
  3306. out:
  3307. mutex_unlock(&dentry->d_inode->i_mutex);
  3308. dput(dentry);
  3309. if (!error)
  3310. d_delete(dentry);
  3311. return error;
  3312. }
  3313. EXPORT_SYMBOL(vfs_rmdir);
  3314. static long do_rmdir(int dfd, const char __user *pathname)
  3315. {
  3316. int error = 0;
  3317. struct filename *name;
  3318. struct dentry *dentry;
  3319. struct path path;
  3320. struct qstr last;
  3321. int type;
  3322. unsigned int lookup_flags = 0;
  3323. retry:
  3324. name = user_path_parent(dfd, pathname,
  3325. &path, &last, &type, lookup_flags);
  3326. if (IS_ERR(name))
  3327. return PTR_ERR(name);
  3328. switch (type) {
  3329. case LAST_DOTDOT:
  3330. error = -ENOTEMPTY;
  3331. goto exit1;
  3332. case LAST_DOT:
  3333. error = -EINVAL;
  3334. goto exit1;
  3335. case LAST_ROOT:
  3336. error = -EBUSY;
  3337. goto exit1;
  3338. }
  3339. error = mnt_want_write(path.mnt);
  3340. if (error)
  3341. goto exit1;
  3342. mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3343. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3344. error = PTR_ERR(dentry);
  3345. if (IS_ERR(dentry))
  3346. goto exit2;
  3347. if (!dentry->d_inode) {
  3348. error = -ENOENT;
  3349. goto exit3;
  3350. }
  3351. error = security_path_rmdir(&path, dentry);
  3352. if (error)
  3353. goto exit3;
  3354. error = vfs_rmdir(path.dentry->d_inode, dentry);
  3355. exit3:
  3356. dput(dentry);
  3357. exit2:
  3358. mutex_unlock(&path.dentry->d_inode->i_mutex);
  3359. mnt_drop_write(path.mnt);
  3360. exit1:
  3361. path_put(&path);
  3362. putname(name);
  3363. if (retry_estale(error, lookup_flags)) {
  3364. lookup_flags |= LOOKUP_REVAL;
  3365. goto retry;
  3366. }
  3367. return error;
  3368. }
  3369. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  3370. {
  3371. return do_rmdir(AT_FDCWD, pathname);
  3372. }
  3373. /**
  3374. * vfs_unlink - unlink a filesystem object
  3375. * @dir: parent directory
  3376. * @dentry: victim
  3377. * @delegated_inode: returns victim inode, if the inode is delegated.
  3378. *
  3379. * The caller must hold dir->i_mutex.
  3380. *
  3381. * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
  3382. * return a reference to the inode in delegated_inode. The caller
  3383. * should then break the delegation on that inode and retry. Because
  3384. * breaking a delegation may take a long time, the caller should drop
  3385. * dir->i_mutex before doing so.
  3386. *
  3387. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3388. * be appropriate for callers that expect the underlying filesystem not
  3389. * to be NFS exported.
  3390. */
  3391. int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
  3392. {
  3393. struct inode *target = dentry->d_inode;
  3394. int error = may_delete(dir, dentry, 0);
  3395. if (error)
  3396. return error;
  3397. if (!dir->i_op->unlink)
  3398. return -EPERM;
  3399. mutex_lock(&target->i_mutex);
  3400. if (is_local_mountpoint(dentry))
  3401. error = -EBUSY;
  3402. else {
  3403. error = security_inode_unlink(dir, dentry);
  3404. if (!error) {
  3405. error = try_break_deleg(target, delegated_inode);
  3406. if (error)
  3407. goto out;
  3408. error = dir->i_op->unlink(dir, dentry);
  3409. if (!error) {
  3410. dont_mount(dentry);
  3411. detach_mounts(dentry);
  3412. }
  3413. }
  3414. }
  3415. out:
  3416. mutex_unlock(&target->i_mutex);
  3417. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  3418. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  3419. fsnotify_link_count(target);
  3420. d_delete(dentry);
  3421. }
  3422. return error;
  3423. }
  3424. EXPORT_SYMBOL(vfs_unlink);
  3425. /*
  3426. * Make sure that the actual truncation of the file will occur outside its
  3427. * directory's i_mutex. Truncate can take a long time if there is a lot of
  3428. * writeout happening, and we don't want to prevent access to the directory
  3429. * while waiting on the I/O.
  3430. */
  3431. static long do_unlinkat(int dfd, const char __user *pathname)
  3432. {
  3433. int error;
  3434. struct filename *name;
  3435. struct dentry *dentry;
  3436. struct path path;
  3437. struct qstr last;
  3438. int type;
  3439. struct inode *inode = NULL;
  3440. struct inode *delegated_inode = NULL;
  3441. unsigned int lookup_flags = 0;
  3442. retry:
  3443. name = user_path_parent(dfd, pathname,
  3444. &path, &last, &type, lookup_flags);
  3445. if (IS_ERR(name))
  3446. return PTR_ERR(name);
  3447. error = -EISDIR;
  3448. if (type != LAST_NORM)
  3449. goto exit1;
  3450. error = mnt_want_write(path.mnt);
  3451. if (error)
  3452. goto exit1;
  3453. retry_deleg:
  3454. mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3455. dentry = __lookup_hash(&last, path.dentry, lookup_flags);
  3456. error = PTR_ERR(dentry);
  3457. if (!IS_ERR(dentry)) {
  3458. /* Why not before? Because we want correct error value */
  3459. if (last.name[last.len])
  3460. goto slashes;
  3461. inode = dentry->d_inode;
  3462. if (d_is_negative(dentry))
  3463. goto slashes;
  3464. ihold(inode);
  3465. error = security_path_unlink(&path, dentry);
  3466. if (error)
  3467. goto exit2;
  3468. error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
  3469. exit2:
  3470. dput(dentry);
  3471. }
  3472. mutex_unlock(&path.dentry->d_inode->i_mutex);
  3473. if (inode)
  3474. iput(inode); /* truncate the inode here */
  3475. inode = NULL;
  3476. if (delegated_inode) {
  3477. error = break_deleg_wait(&delegated_inode);
  3478. if (!error)
  3479. goto retry_deleg;
  3480. }
  3481. mnt_drop_write(path.mnt);
  3482. exit1:
  3483. path_put(&path);
  3484. putname(name);
  3485. if (retry_estale(error, lookup_flags)) {
  3486. lookup_flags |= LOOKUP_REVAL;
  3487. inode = NULL;
  3488. goto retry;
  3489. }
  3490. return error;
  3491. slashes:
  3492. if (d_is_negative(dentry))
  3493. error = -ENOENT;
  3494. else if (d_is_dir(dentry))
  3495. error = -EISDIR;
  3496. else
  3497. error = -ENOTDIR;
  3498. goto exit2;
  3499. }
  3500. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  3501. {
  3502. if ((flag & ~AT_REMOVEDIR) != 0)
  3503. return -EINVAL;
  3504. if (flag & AT_REMOVEDIR)
  3505. return do_rmdir(dfd, pathname);
  3506. return do_unlinkat(dfd, pathname);
  3507. }
  3508. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  3509. {
  3510. return do_unlinkat(AT_FDCWD, pathname);
  3511. }
  3512. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  3513. {
  3514. int error = may_create(dir, dentry);
  3515. if (error)
  3516. return error;
  3517. if (!dir->i_op->symlink)
  3518. return -EPERM;
  3519. error = security_inode_symlink(dir, dentry, oldname);
  3520. if (error)
  3521. return error;
  3522. error = dir->i_op->symlink(dir, dentry, oldname);
  3523. if (!error)
  3524. fsnotify_create(dir, dentry);
  3525. return error;
  3526. }
  3527. EXPORT_SYMBOL(vfs_symlink);
  3528. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  3529. int, newdfd, const char __user *, newname)
  3530. {
  3531. int error;
  3532. struct filename *from;
  3533. struct dentry *dentry;
  3534. struct path path;
  3535. unsigned int lookup_flags = 0;
  3536. from = getname(oldname);
  3537. if (IS_ERR(from))
  3538. return PTR_ERR(from);
  3539. retry:
  3540. dentry = user_path_create(newdfd, newname, &path, lookup_flags);
  3541. error = PTR_ERR(dentry);
  3542. if (IS_ERR(dentry))
  3543. goto out_putname;
  3544. error = security_path_symlink(&path, dentry, from->name);
  3545. if (!error)
  3546. error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
  3547. done_path_create(&path, dentry);
  3548. if (retry_estale(error, lookup_flags)) {
  3549. lookup_flags |= LOOKUP_REVAL;
  3550. goto retry;
  3551. }
  3552. out_putname:
  3553. putname(from);
  3554. return error;
  3555. }
  3556. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  3557. {
  3558. return sys_symlinkat(oldname, AT_FDCWD, newname);
  3559. }
  3560. /**
  3561. * vfs_link - create a new link
  3562. * @old_dentry: object to be linked
  3563. * @dir: new parent
  3564. * @new_dentry: where to create the new link
  3565. * @delegated_inode: returns inode needing a delegation break
  3566. *
  3567. * The caller must hold dir->i_mutex
  3568. *
  3569. * If vfs_link discovers a delegation on the to-be-linked file in need
  3570. * of breaking, it will return -EWOULDBLOCK and return a reference to the
  3571. * inode in delegated_inode. The caller should then break the delegation
  3572. * and retry. Because breaking a delegation may take a long time, the
  3573. * caller should drop the i_mutex before doing so.
  3574. *
  3575. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3576. * be appropriate for callers that expect the underlying filesystem not
  3577. * to be NFS exported.
  3578. */
  3579. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
  3580. {
  3581. struct inode *inode = old_dentry->d_inode;
  3582. unsigned max_links = dir->i_sb->s_max_links;
  3583. int error;
  3584. if (!inode)
  3585. return -ENOENT;
  3586. error = may_create(dir, new_dentry);
  3587. if (error)
  3588. return error;
  3589. if (dir->i_sb != inode->i_sb)
  3590. return -EXDEV;
  3591. /*
  3592. * A link to an append-only or immutable file cannot be created.
  3593. */
  3594. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3595. return -EPERM;
  3596. if (!dir->i_op->link)
  3597. return -EPERM;
  3598. if (S_ISDIR(inode->i_mode))
  3599. return -EPERM;
  3600. error = security_inode_link(old_dentry, dir, new_dentry);
  3601. if (error)
  3602. return error;
  3603. mutex_lock(&inode->i_mutex);
  3604. /* Make sure we don't allow creating hardlink to an unlinked file */
  3605. if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
  3606. error = -ENOENT;
  3607. else if (max_links && inode->i_nlink >= max_links)
  3608. error = -EMLINK;
  3609. else {
  3610. error = try_break_deleg(inode, delegated_inode);
  3611. if (!error)
  3612. error = dir->i_op->link(old_dentry, dir, new_dentry);
  3613. }
  3614. if (!error && (inode->i_state & I_LINKABLE)) {
  3615. spin_lock(&inode->i_lock);
  3616. inode->i_state &= ~I_LINKABLE;
  3617. spin_unlock(&inode->i_lock);
  3618. }
  3619. mutex_unlock(&inode->i_mutex);
  3620. if (!error)
  3621. fsnotify_link(dir, inode, new_dentry);
  3622. return error;
  3623. }
  3624. EXPORT_SYMBOL(vfs_link);
  3625. /*
  3626. * Hardlinks are often used in delicate situations. We avoid
  3627. * security-related surprises by not following symlinks on the
  3628. * newname. --KAB
  3629. *
  3630. * We don't follow them on the oldname either to be compatible
  3631. * with linux 2.0, and to avoid hard-linking to directories
  3632. * and other special files. --ADM
  3633. */
  3634. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  3635. int, newdfd, const char __user *, newname, int, flags)
  3636. {
  3637. struct dentry *new_dentry;
  3638. struct path old_path, new_path;
  3639. struct inode *delegated_inode = NULL;
  3640. int how = 0;
  3641. int error;
  3642. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  3643. return -EINVAL;
  3644. /*
  3645. * To use null names we require CAP_DAC_READ_SEARCH
  3646. * This ensures that not everyone will be able to create
  3647. * handlink using the passed filedescriptor.
  3648. */
  3649. if (flags & AT_EMPTY_PATH) {
  3650. if (!capable(CAP_DAC_READ_SEARCH))
  3651. return -ENOENT;
  3652. how = LOOKUP_EMPTY;
  3653. }
  3654. if (flags & AT_SYMLINK_FOLLOW)
  3655. how |= LOOKUP_FOLLOW;
  3656. retry:
  3657. error = user_path_at(olddfd, oldname, how, &old_path);
  3658. if (error)
  3659. return error;
  3660. new_dentry = user_path_create(newdfd, newname, &new_path,
  3661. (how & LOOKUP_REVAL));
  3662. error = PTR_ERR(new_dentry);
  3663. if (IS_ERR(new_dentry))
  3664. goto out;
  3665. error = -EXDEV;
  3666. if (old_path.mnt != new_path.mnt)
  3667. goto out_dput;
  3668. error = may_linkat(&old_path);
  3669. if (unlikely(error))
  3670. goto out_dput;
  3671. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  3672. if (error)
  3673. goto out_dput;
  3674. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
  3675. out_dput:
  3676. done_path_create(&new_path, new_dentry);
  3677. if (delegated_inode) {
  3678. error = break_deleg_wait(&delegated_inode);
  3679. if (!error) {
  3680. path_put(&old_path);
  3681. goto retry;
  3682. }
  3683. }
  3684. if (retry_estale(error, how)) {
  3685. path_put(&old_path);
  3686. how |= LOOKUP_REVAL;
  3687. goto retry;
  3688. }
  3689. out:
  3690. path_put(&old_path);
  3691. return error;
  3692. }
  3693. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  3694. {
  3695. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3696. }
  3697. /**
  3698. * vfs_rename - rename a filesystem object
  3699. * @old_dir: parent of source
  3700. * @old_dentry: source
  3701. * @new_dir: parent of destination
  3702. * @new_dentry: destination
  3703. * @delegated_inode: returns an inode needing a delegation break
  3704. * @flags: rename flags
  3705. *
  3706. * The caller must hold multiple mutexes--see lock_rename()).
  3707. *
  3708. * If vfs_rename discovers a delegation in need of breaking at either
  3709. * the source or destination, it will return -EWOULDBLOCK and return a
  3710. * reference to the inode in delegated_inode. The caller should then
  3711. * break the delegation and retry. Because breaking a delegation may
  3712. * take a long time, the caller should drop all locks before doing
  3713. * so.
  3714. *
  3715. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3716. * be appropriate for callers that expect the underlying filesystem not
  3717. * to be NFS exported.
  3718. *
  3719. * The worst of all namespace operations - renaming directory. "Perverted"
  3720. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  3721. * Problems:
  3722. * a) we can get into loop creation.
  3723. * b) race potential - two innocent renames can create a loop together.
  3724. * That's where 4.4 screws up. Current fix: serialization on
  3725. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  3726. * story.
  3727. * c) we have to lock _four_ objects - parents and victim (if it exists),
  3728. * and source (if it is not a directory).
  3729. * And that - after we got ->i_mutex on parents (until then we don't know
  3730. * whether the target exists). Solution: try to be smart with locking
  3731. * order for inodes. We rely on the fact that tree topology may change
  3732. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  3733. * move will be locked. Thus we can rank directories by the tree
  3734. * (ancestors first) and rank all non-directories after them.
  3735. * That works since everybody except rename does "lock parent, lookup,
  3736. * lock child" and rename is under ->s_vfs_rename_mutex.
  3737. * HOWEVER, it relies on the assumption that any object with ->lookup()
  3738. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  3739. * we'd better make sure that there's no link(2) for them.
  3740. * d) conversion from fhandle to dentry may come in the wrong moment - when
  3741. * we are removing the target. Solution: we will have to grab ->i_mutex
  3742. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  3743. * ->i_mutex on parents, which works but leads to some truly excessive
  3744. * locking].
  3745. */
  3746. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  3747. struct inode *new_dir, struct dentry *new_dentry,
  3748. struct inode **delegated_inode, unsigned int flags)
  3749. {
  3750. int error;
  3751. bool is_dir = d_is_dir(old_dentry);
  3752. const unsigned char *old_name;
  3753. struct inode *source = old_dentry->d_inode;
  3754. struct inode *target = new_dentry->d_inode;
  3755. bool new_is_dir = false;
  3756. unsigned max_links = new_dir->i_sb->s_max_links;
  3757. if (source == target)
  3758. return 0;
  3759. error = may_delete(old_dir, old_dentry, is_dir);
  3760. if (error)
  3761. return error;
  3762. if (!target) {
  3763. error = may_create(new_dir, new_dentry);
  3764. } else {
  3765. new_is_dir = d_is_dir(new_dentry);
  3766. if (!(flags & RENAME_EXCHANGE))
  3767. error = may_delete(new_dir, new_dentry, is_dir);
  3768. else
  3769. error = may_delete(new_dir, new_dentry, new_is_dir);
  3770. }
  3771. if (error)
  3772. return error;
  3773. if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
  3774. return -EPERM;
  3775. if (flags && !old_dir->i_op->rename2)
  3776. return -EINVAL;
  3777. /*
  3778. * If we are going to change the parent - check write permissions,
  3779. * we'll need to flip '..'.
  3780. */
  3781. if (new_dir != old_dir) {
  3782. if (is_dir) {
  3783. error = inode_permission(source, MAY_WRITE);
  3784. if (error)
  3785. return error;
  3786. }
  3787. if ((flags & RENAME_EXCHANGE) && new_is_dir) {
  3788. error = inode_permission(target, MAY_WRITE);
  3789. if (error)
  3790. return error;
  3791. }
  3792. }
  3793. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
  3794. flags);
  3795. if (error)
  3796. return error;
  3797. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  3798. dget(new_dentry);
  3799. if (!is_dir || (flags & RENAME_EXCHANGE))
  3800. lock_two_nondirectories(source, target);
  3801. else if (target)
  3802. mutex_lock(&target->i_mutex);
  3803. error = -EBUSY;
  3804. if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
  3805. goto out;
  3806. if (max_links && new_dir != old_dir) {
  3807. error = -EMLINK;
  3808. if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
  3809. goto out;
  3810. if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
  3811. old_dir->i_nlink >= max_links)
  3812. goto out;
  3813. }
  3814. if (is_dir && !(flags & RENAME_EXCHANGE) && target)
  3815. shrink_dcache_parent(new_dentry);
  3816. if (!is_dir) {
  3817. error = try_break_deleg(source, delegated_inode);
  3818. if (error)
  3819. goto out;
  3820. }
  3821. if (target && !new_is_dir) {
  3822. error = try_break_deleg(target, delegated_inode);
  3823. if (error)
  3824. goto out;
  3825. }
  3826. if (!old_dir->i_op->rename2) {
  3827. error = old_dir->i_op->rename(old_dir, old_dentry,
  3828. new_dir, new_dentry);
  3829. } else {
  3830. WARN_ON(old_dir->i_op->rename != NULL);
  3831. error = old_dir->i_op->rename2(old_dir, old_dentry,
  3832. new_dir, new_dentry, flags);
  3833. }
  3834. if (error)
  3835. goto out;
  3836. if (!(flags & RENAME_EXCHANGE) && target) {
  3837. if (is_dir)
  3838. target->i_flags |= S_DEAD;
  3839. dont_mount(new_dentry);
  3840. detach_mounts(new_dentry);
  3841. }
  3842. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
  3843. if (!(flags & RENAME_EXCHANGE))
  3844. d_move(old_dentry, new_dentry);
  3845. else
  3846. d_exchange(old_dentry, new_dentry);
  3847. }
  3848. out:
  3849. if (!is_dir || (flags & RENAME_EXCHANGE))
  3850. unlock_two_nondirectories(source, target);
  3851. else if (target)
  3852. mutex_unlock(&target->i_mutex);
  3853. dput(new_dentry);
  3854. if (!error) {
  3855. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  3856. !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
  3857. if (flags & RENAME_EXCHANGE) {
  3858. fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
  3859. new_is_dir, NULL, new_dentry);
  3860. }
  3861. }
  3862. fsnotify_oldname_free(old_name);
  3863. return error;
  3864. }
  3865. EXPORT_SYMBOL(vfs_rename);
  3866. SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
  3867. int, newdfd, const char __user *, newname, unsigned int, flags)
  3868. {
  3869. struct dentry *old_dentry, *new_dentry;
  3870. struct dentry *trap;
  3871. struct path old_path, new_path;
  3872. struct qstr old_last, new_last;
  3873. int old_type, new_type;
  3874. struct inode *delegated_inode = NULL;
  3875. struct filename *from;
  3876. struct filename *to;
  3877. unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
  3878. bool should_retry = false;
  3879. int error;
  3880. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  3881. return -EINVAL;
  3882. if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
  3883. (flags & RENAME_EXCHANGE))
  3884. return -EINVAL;
  3885. if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
  3886. return -EPERM;
  3887. if (flags & RENAME_EXCHANGE)
  3888. target_flags = 0;
  3889. retry:
  3890. from = user_path_parent(olddfd, oldname,
  3891. &old_path, &old_last, &old_type, lookup_flags);
  3892. if (IS_ERR(from)) {
  3893. error = PTR_ERR(from);
  3894. goto exit;
  3895. }
  3896. to = user_path_parent(newdfd, newname,
  3897. &new_path, &new_last, &new_type, lookup_flags);
  3898. if (IS_ERR(to)) {
  3899. error = PTR_ERR(to);
  3900. goto exit1;
  3901. }
  3902. error = -EXDEV;
  3903. if (old_path.mnt != new_path.mnt)
  3904. goto exit2;
  3905. error = -EBUSY;
  3906. if (old_type != LAST_NORM)
  3907. goto exit2;
  3908. if (flags & RENAME_NOREPLACE)
  3909. error = -EEXIST;
  3910. if (new_type != LAST_NORM)
  3911. goto exit2;
  3912. error = mnt_want_write(old_path.mnt);
  3913. if (error)
  3914. goto exit2;
  3915. retry_deleg:
  3916. trap = lock_rename(new_path.dentry, old_path.dentry);
  3917. old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
  3918. error = PTR_ERR(old_dentry);
  3919. if (IS_ERR(old_dentry))
  3920. goto exit3;
  3921. /* source must exist */
  3922. error = -ENOENT;
  3923. if (d_is_negative(old_dentry))
  3924. goto exit4;
  3925. new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
  3926. error = PTR_ERR(new_dentry);
  3927. if (IS_ERR(new_dentry))
  3928. goto exit4;
  3929. error = -EEXIST;
  3930. if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
  3931. goto exit5;
  3932. if (flags & RENAME_EXCHANGE) {
  3933. error = -ENOENT;
  3934. if (d_is_negative(new_dentry))
  3935. goto exit5;
  3936. if (!d_is_dir(new_dentry)) {
  3937. error = -ENOTDIR;
  3938. if (new_last.name[new_last.len])
  3939. goto exit5;
  3940. }
  3941. }
  3942. /* unless the source is a directory trailing slashes give -ENOTDIR */
  3943. if (!d_is_dir(old_dentry)) {
  3944. error = -ENOTDIR;
  3945. if (old_last.name[old_last.len])
  3946. goto exit5;
  3947. if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
  3948. goto exit5;
  3949. }
  3950. /* source should not be ancestor of target */
  3951. error = -EINVAL;
  3952. if (old_dentry == trap)
  3953. goto exit5;
  3954. /* target should not be an ancestor of source */
  3955. if (!(flags & RENAME_EXCHANGE))
  3956. error = -ENOTEMPTY;
  3957. if (new_dentry == trap)
  3958. goto exit5;
  3959. error = security_path_rename(&old_path, old_dentry,
  3960. &new_path, new_dentry, flags);
  3961. if (error)
  3962. goto exit5;
  3963. error = vfs_rename(old_path.dentry->d_inode, old_dentry,
  3964. new_path.dentry->d_inode, new_dentry,
  3965. &delegated_inode, flags);
  3966. exit5:
  3967. dput(new_dentry);
  3968. exit4:
  3969. dput(old_dentry);
  3970. exit3:
  3971. unlock_rename(new_path.dentry, old_path.dentry);
  3972. if (delegated_inode) {
  3973. error = break_deleg_wait(&delegated_inode);
  3974. if (!error)
  3975. goto retry_deleg;
  3976. }
  3977. mnt_drop_write(old_path.mnt);
  3978. exit2:
  3979. if (retry_estale(error, lookup_flags))
  3980. should_retry = true;
  3981. path_put(&new_path);
  3982. putname(to);
  3983. exit1:
  3984. path_put(&old_path);
  3985. putname(from);
  3986. if (should_retry) {
  3987. should_retry = false;
  3988. lookup_flags |= LOOKUP_REVAL;
  3989. goto retry;
  3990. }
  3991. exit:
  3992. return error;
  3993. }
  3994. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  3995. int, newdfd, const char __user *, newname)
  3996. {
  3997. return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
  3998. }
  3999. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  4000. {
  4001. return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  4002. }
  4003. int vfs_whiteout(struct inode *dir, struct dentry *dentry)
  4004. {
  4005. int error = may_create(dir, dentry);
  4006. if (error)
  4007. return error;
  4008. if (!dir->i_op->mknod)
  4009. return -EPERM;
  4010. return dir->i_op->mknod(dir, dentry,
  4011. S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
  4012. }
  4013. EXPORT_SYMBOL(vfs_whiteout);
  4014. int readlink_copy(char __user *buffer, int buflen, const char *link)
  4015. {
  4016. int len = PTR_ERR(link);
  4017. if (IS_ERR(link))
  4018. goto out;
  4019. len = strlen(link);
  4020. if (len > (unsigned) buflen)
  4021. len = buflen;
  4022. if (copy_to_user(buffer, link, len))
  4023. len = -EFAULT;
  4024. out:
  4025. return len;
  4026. }
  4027. EXPORT_SYMBOL(readlink_copy);
  4028. /*
  4029. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  4030. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  4031. * using) it for any given inode is up to filesystem.
  4032. */
  4033. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4034. {
  4035. void *cookie;
  4036. struct inode *inode = d_inode(dentry);
  4037. const char *link = inode->i_link;
  4038. int res;
  4039. if (!link) {
  4040. link = inode->i_op->follow_link(dentry, &cookie);
  4041. if (IS_ERR(link))
  4042. return PTR_ERR(link);
  4043. }
  4044. res = readlink_copy(buffer, buflen, link);
  4045. if (inode->i_op->put_link)
  4046. inode->i_op->put_link(inode, cookie);
  4047. return res;
  4048. }
  4049. EXPORT_SYMBOL(generic_readlink);
  4050. /* get the link contents into pagecache */
  4051. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  4052. {
  4053. char *kaddr;
  4054. struct page *page;
  4055. struct address_space *mapping = dentry->d_inode->i_mapping;
  4056. page = read_mapping_page(mapping, 0, NULL);
  4057. if (IS_ERR(page))
  4058. return (char*)page;
  4059. *ppage = page;
  4060. kaddr = kmap(page);
  4061. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  4062. return kaddr;
  4063. }
  4064. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  4065. {
  4066. struct page *page = NULL;
  4067. int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
  4068. if (page) {
  4069. kunmap(page);
  4070. page_cache_release(page);
  4071. }
  4072. return res;
  4073. }
  4074. EXPORT_SYMBOL(page_readlink);
  4075. const char *page_follow_link_light(struct dentry *dentry, void **cookie)
  4076. {
  4077. struct page *page = NULL;
  4078. char *res = page_getlink(dentry, &page);
  4079. if (!IS_ERR(res))
  4080. *cookie = page;
  4081. return res;
  4082. }
  4083. EXPORT_SYMBOL(page_follow_link_light);
  4084. void page_put_link(struct inode *unused, void *cookie)
  4085. {
  4086. struct page *page = cookie;
  4087. kunmap(page);
  4088. page_cache_release(page);
  4089. }
  4090. EXPORT_SYMBOL(page_put_link);
  4091. /*
  4092. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  4093. */
  4094. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  4095. {
  4096. struct address_space *mapping = inode->i_mapping;
  4097. struct page *page;
  4098. void *fsdata;
  4099. int err;
  4100. char *kaddr;
  4101. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  4102. if (nofs)
  4103. flags |= AOP_FLAG_NOFS;
  4104. retry:
  4105. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  4106. flags, &page, &fsdata);
  4107. if (err)
  4108. goto fail;
  4109. kaddr = kmap_atomic(page);
  4110. memcpy(kaddr, symname, len-1);
  4111. kunmap_atomic(kaddr);
  4112. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  4113. page, fsdata);
  4114. if (err < 0)
  4115. goto fail;
  4116. if (err < len-1)
  4117. goto retry;
  4118. mark_inode_dirty(inode);
  4119. return 0;
  4120. fail:
  4121. return err;
  4122. }
  4123. EXPORT_SYMBOL(__page_symlink);
  4124. int page_symlink(struct inode *inode, const char *symname, int len)
  4125. {
  4126. return __page_symlink(inode, symname, len,
  4127. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  4128. }
  4129. EXPORT_SYMBOL(page_symlink);
  4130. const struct inode_operations page_symlink_inode_operations = {
  4131. .readlink = generic_readlink,
  4132. .follow_link = page_follow_link_light,
  4133. .put_link = page_put_link,
  4134. };
  4135. EXPORT_SYMBOL(page_symlink_inode_operations);