namei.c 113 KB

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