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 - sizeof(struct filename))
  113. struct filename *
  114. getname_flags(const char __user *filename, int flags, int *empty)
  115. {
  116. struct filename *result, *err;
  117. int len;
  118. long max;
  119. char *kname;
  120. result = audit_reusename(filename);
  121. if (result)
  122. return result;
  123. result = __getname();
  124. if (unlikely(!result))
  125. return ERR_PTR(-ENOMEM);
  126. result->refcnt = 1;
  127. /*
  128. * First, try to embed the struct filename inside the names_cache
  129. * allocation
  130. */
  131. kname = (char *)result + sizeof(*result);
  132. result->name = kname;
  133. result->separate = false;
  134. max = EMBEDDED_NAME_MAX;
  135. recopy:
  136. len = strncpy_from_user(kname, filename, max);
  137. if (unlikely(len < 0)) {
  138. err = ERR_PTR(len);
  139. goto error;
  140. }
  141. /*
  142. * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
  143. * separate struct filename so we can dedicate the entire
  144. * names_cache allocation for the pathname, and re-do the copy from
  145. * userland.
  146. */
  147. if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
  148. kname = (char *)result;
  149. result = kzalloc(sizeof(*result), GFP_KERNEL);
  150. if (!result) {
  151. err = ERR_PTR(-ENOMEM);
  152. result = (struct filename *)kname;
  153. goto error;
  154. }
  155. result->name = kname;
  156. result->separate = true;
  157. result->refcnt = 1;
  158. max = PATH_MAX;
  159. goto recopy;
  160. }
  161. /* The empty path is special. */
  162. if (unlikely(!len)) {
  163. if (empty)
  164. *empty = 1;
  165. err = ERR_PTR(-ENOENT);
  166. if (!(flags & LOOKUP_EMPTY))
  167. goto error;
  168. }
  169. err = ERR_PTR(-ENAMETOOLONG);
  170. if (unlikely(len >= PATH_MAX))
  171. goto error;
  172. result->uptr = filename;
  173. result->aname = NULL;
  174. audit_getname(result);
  175. return result;
  176. error:
  177. putname(result);
  178. return err;
  179. }
  180. struct filename *
  181. getname(const char __user * filename)
  182. {
  183. return getname_flags(filename, 0, NULL);
  184. }
  185. struct filename *
  186. getname_kernel(const char * filename)
  187. {
  188. struct filename *result;
  189. int len = strlen(filename) + 1;
  190. result = __getname();
  191. if (unlikely(!result))
  192. return ERR_PTR(-ENOMEM);
  193. if (len <= EMBEDDED_NAME_MAX) {
  194. result->name = (char *)(result) + sizeof(*result);
  195. result->separate = false;
  196. } else if (len <= PATH_MAX) {
  197. struct filename *tmp;
  198. tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
  199. if (unlikely(!tmp)) {
  200. __putname(result);
  201. return ERR_PTR(-ENOMEM);
  202. }
  203. tmp->name = (char *)result;
  204. tmp->separate = true;
  205. result = tmp;
  206. } else {
  207. __putname(result);
  208. return ERR_PTR(-ENAMETOOLONG);
  209. }
  210. memcpy((char *)result->name, filename, len);
  211. result->uptr = NULL;
  212. result->aname = NULL;
  213. result->refcnt = 1;
  214. audit_getname(result);
  215. return result;
  216. }
  217. void putname(struct filename *name)
  218. {
  219. BUG_ON(name->refcnt <= 0);
  220. if (--name->refcnt > 0)
  221. return;
  222. if (name->separate) {
  223. __putname(name->name);
  224. kfree(name);
  225. } else
  226. __putname(name);
  227. }
  228. static int check_acl(struct inode *inode, int mask)
  229. {
  230. #ifdef CONFIG_FS_POSIX_ACL
  231. struct posix_acl *acl;
  232. if (mask & MAY_NOT_BLOCK) {
  233. acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  234. if (!acl)
  235. return -EAGAIN;
  236. /* no ->get_acl() calls in RCU mode... */
  237. if (acl == ACL_NOT_CACHED)
  238. return -ECHILD;
  239. return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
  240. }
  241. acl = get_acl(inode, ACL_TYPE_ACCESS);
  242. if (IS_ERR(acl))
  243. return PTR_ERR(acl);
  244. if (acl) {
  245. int error = posix_acl_permission(inode, acl, mask);
  246. posix_acl_release(acl);
  247. return error;
  248. }
  249. #endif
  250. return -EAGAIN;
  251. }
  252. /*
  253. * This does the basic permission checking
  254. */
  255. static int acl_permission_check(struct inode *inode, int mask)
  256. {
  257. unsigned int mode = inode->i_mode;
  258. if (likely(uid_eq(current_fsuid(), inode->i_uid)))
  259. mode >>= 6;
  260. else {
  261. if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
  262. int error = check_acl(inode, mask);
  263. if (error != -EAGAIN)
  264. return error;
  265. }
  266. if (in_group_p(inode->i_gid))
  267. mode >>= 3;
  268. }
  269. /*
  270. * If the DACs are ok we don't need any capability check.
  271. */
  272. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  273. return 0;
  274. return -EACCES;
  275. }
  276. /**
  277. * generic_permission - check for access rights on a Posix-like filesystem
  278. * @inode: inode to check access rights for
  279. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
  280. *
  281. * Used to check for read/write/execute permissions on a file.
  282. * We use "fsuid" for this, letting us set arbitrary permissions
  283. * for filesystem access without changing the "normal" uids which
  284. * are used for other things.
  285. *
  286. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  287. * request cannot be satisfied (eg. requires blocking or too much complexity).
  288. * It would then be called again in ref-walk mode.
  289. */
  290. int generic_permission(struct inode *inode, int mask)
  291. {
  292. int ret;
  293. /*
  294. * Do the basic permission checks.
  295. */
  296. ret = acl_permission_check(inode, mask);
  297. if (ret != -EACCES)
  298. return ret;
  299. if (S_ISDIR(inode->i_mode)) {
  300. /* DACs are overridable for directories */
  301. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  302. return 0;
  303. if (!(mask & MAY_WRITE))
  304. if (capable_wrt_inode_uidgid(inode,
  305. CAP_DAC_READ_SEARCH))
  306. return 0;
  307. return -EACCES;
  308. }
  309. /*
  310. * Read/write DACs are always overridable.
  311. * Executable DACs are overridable when there is
  312. * at least one exec bit set.
  313. */
  314. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  315. if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
  316. return 0;
  317. /*
  318. * Searching includes executable on directories, else just read.
  319. */
  320. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  321. if (mask == MAY_READ)
  322. if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
  323. return 0;
  324. return -EACCES;
  325. }
  326. EXPORT_SYMBOL(generic_permission);
  327. /*
  328. * We _really_ want to just do "generic_permission()" without
  329. * even looking at the inode->i_op values. So we keep a cache
  330. * flag in inode->i_opflags, that says "this has not special
  331. * permission function, use the fast case".
  332. */
  333. static inline int do_inode_permission(struct inode *inode, int mask)
  334. {
  335. if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  336. if (likely(inode->i_op->permission))
  337. return inode->i_op->permission(inode, mask);
  338. /* This gets set once for the inode lifetime */
  339. spin_lock(&inode->i_lock);
  340. inode->i_opflags |= IOP_FASTPERM;
  341. spin_unlock(&inode->i_lock);
  342. }
  343. return generic_permission(inode, mask);
  344. }
  345. /**
  346. * __inode_permission - Check for access rights to a given inode
  347. * @inode: Inode to check permission on
  348. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  349. *
  350. * Check for read/write/execute permissions on an inode.
  351. *
  352. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  353. *
  354. * This does not check for a read-only file system. You probably want
  355. * inode_permission().
  356. */
  357. int __inode_permission(struct inode *inode, int mask)
  358. {
  359. int retval;
  360. if (unlikely(mask & MAY_WRITE)) {
  361. /*
  362. * Nobody gets write access to an immutable file.
  363. */
  364. if (IS_IMMUTABLE(inode))
  365. return -EACCES;
  366. }
  367. retval = do_inode_permission(inode, mask);
  368. if (retval)
  369. return retval;
  370. retval = devcgroup_inode_permission(inode, mask);
  371. if (retval)
  372. return retval;
  373. return security_inode_permission(inode, mask);
  374. }
  375. EXPORT_SYMBOL(__inode_permission);
  376. /**
  377. * sb_permission - Check superblock-level permissions
  378. * @sb: Superblock of inode to check permission on
  379. * @inode: Inode to check permission on
  380. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  381. *
  382. * Separate out file-system wide checks from inode-specific permission checks.
  383. */
  384. static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
  385. {
  386. if (unlikely(mask & MAY_WRITE)) {
  387. umode_t mode = inode->i_mode;
  388. /* Nobody gets write access to a read-only fs. */
  389. if ((sb->s_flags & MS_RDONLY) &&
  390. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  391. return -EROFS;
  392. }
  393. return 0;
  394. }
  395. /**
  396. * inode_permission - Check for access rights to a given inode
  397. * @inode: Inode to check permission on
  398. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  399. *
  400. * Check for read/write/execute permissions on an inode. We use fs[ug]id for
  401. * this, letting us set arbitrary permissions for filesystem access without
  402. * changing the "normal" UIDs which are used for other things.
  403. *
  404. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  405. */
  406. int inode_permission(struct inode *inode, int mask)
  407. {
  408. int retval;
  409. retval = sb_permission(inode->i_sb, inode, mask);
  410. if (retval)
  411. return retval;
  412. return __inode_permission(inode, mask);
  413. }
  414. EXPORT_SYMBOL(inode_permission);
  415. /**
  416. * path_get - get a reference to a path
  417. * @path: path to get the reference to
  418. *
  419. * Given a path increment the reference count to the dentry and the vfsmount.
  420. */
  421. void path_get(const struct path *path)
  422. {
  423. mntget(path->mnt);
  424. dget(path->dentry);
  425. }
  426. EXPORT_SYMBOL(path_get);
  427. /**
  428. * path_put - put a reference to a path
  429. * @path: path to put the reference to
  430. *
  431. * Given a path decrement the reference count to the dentry and the vfsmount.
  432. */
  433. void path_put(const struct path *path)
  434. {
  435. dput(path->dentry);
  436. mntput(path->mnt);
  437. }
  438. EXPORT_SYMBOL(path_put);
  439. struct nameidata {
  440. struct path path;
  441. struct qstr last;
  442. struct path root;
  443. struct inode *inode; /* path.dentry.d_inode */
  444. unsigned int flags;
  445. unsigned seq, m_seq;
  446. int last_type;
  447. unsigned depth;
  448. struct file *base;
  449. char *saved_names[MAX_NESTED_LINKS + 1];
  450. };
  451. /*
  452. * Path walking has 2 modes, rcu-walk and ref-walk (see
  453. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  454. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  455. * normal reference counts on dentries and vfsmounts to transition to rcu-walk
  456. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  457. * got stuck, so ref-walk may continue from there. If this is not successful
  458. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  459. * to restart the path walk from the beginning in ref-walk mode.
  460. */
  461. /**
  462. * unlazy_walk - try to switch to ref-walk mode.
  463. * @nd: nameidata pathwalk data
  464. * @dentry: child of nd->path.dentry or NULL
  465. * Returns: 0 on success, -ECHILD on failure
  466. *
  467. * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
  468. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  469. * @nd or NULL. Must be called from rcu-walk context.
  470. */
  471. static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
  472. {
  473. struct fs_struct *fs = current->fs;
  474. struct dentry *parent = nd->path.dentry;
  475. BUG_ON(!(nd->flags & LOOKUP_RCU));
  476. /*
  477. * After legitimizing the bastards, terminate_walk()
  478. * will do the right thing for non-RCU mode, and all our
  479. * subsequent exit cases should rcu_read_unlock()
  480. * before returning. Do vfsmount first; if dentry
  481. * can't be legitimized, just set nd->path.dentry to NULL
  482. * and rely on dput(NULL) being a no-op.
  483. */
  484. if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
  485. return -ECHILD;
  486. nd->flags &= ~LOOKUP_RCU;
  487. if (!lockref_get_not_dead(&parent->d_lockref)) {
  488. nd->path.dentry = NULL;
  489. goto out;
  490. }
  491. /*
  492. * For a negative lookup, the lookup sequence point is the parents
  493. * sequence point, and it only needs to revalidate the parent dentry.
  494. *
  495. * For a positive lookup, we need to move both the parent and the
  496. * dentry from the RCU domain to be properly refcounted. And the
  497. * sequence number in the dentry validates *both* dentry counters,
  498. * since we checked the sequence number of the parent after we got
  499. * the child sequence number. So we know the parent must still
  500. * be valid if the child sequence number is still valid.
  501. */
  502. if (!dentry) {
  503. if (read_seqcount_retry(&parent->d_seq, nd->seq))
  504. goto out;
  505. BUG_ON(nd->inode != parent->d_inode);
  506. } else {
  507. if (!lockref_get_not_dead(&dentry->d_lockref))
  508. goto out;
  509. if (read_seqcount_retry(&dentry->d_seq, nd->seq))
  510. goto drop_dentry;
  511. }
  512. /*
  513. * Sequence counts matched. Now make sure that the root is
  514. * still valid and get it if required.
  515. */
  516. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  517. spin_lock(&fs->lock);
  518. if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
  519. goto unlock_and_drop_dentry;
  520. path_get(&nd->root);
  521. spin_unlock(&fs->lock);
  522. }
  523. rcu_read_unlock();
  524. return 0;
  525. unlock_and_drop_dentry:
  526. spin_unlock(&fs->lock);
  527. drop_dentry:
  528. rcu_read_unlock();
  529. dput(dentry);
  530. goto drop_root_mnt;
  531. out:
  532. rcu_read_unlock();
  533. drop_root_mnt:
  534. if (!(nd->flags & LOOKUP_ROOT))
  535. nd->root.mnt = NULL;
  536. return -ECHILD;
  537. }
  538. static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
  539. {
  540. return dentry->d_op->d_revalidate(dentry, flags);
  541. }
  542. /**
  543. * complete_walk - successful completion of path walk
  544. * @nd: pointer nameidata
  545. *
  546. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  547. * Revalidate the final result, unless we'd already done that during
  548. * the path walk or the filesystem doesn't ask for it. Return 0 on
  549. * success, -error on failure. In case of failure caller does not
  550. * need to drop nd->path.
  551. */
  552. static int complete_walk(struct nameidata *nd)
  553. {
  554. struct dentry *dentry = nd->path.dentry;
  555. int status;
  556. if (nd->flags & LOOKUP_RCU) {
  557. nd->flags &= ~LOOKUP_RCU;
  558. if (!(nd->flags & LOOKUP_ROOT))
  559. nd->root.mnt = NULL;
  560. if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
  561. rcu_read_unlock();
  562. return -ECHILD;
  563. }
  564. if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
  565. rcu_read_unlock();
  566. mntput(nd->path.mnt);
  567. return -ECHILD;
  568. }
  569. if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
  570. rcu_read_unlock();
  571. dput(dentry);
  572. mntput(nd->path.mnt);
  573. return -ECHILD;
  574. }
  575. rcu_read_unlock();
  576. }
  577. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  578. return 0;
  579. if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
  580. return 0;
  581. status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
  582. if (status > 0)
  583. return 0;
  584. if (!status)
  585. status = -ESTALE;
  586. path_put(&nd->path);
  587. return status;
  588. }
  589. static __always_inline void set_root(struct nameidata *nd)
  590. {
  591. get_fs_root(current->fs, &nd->root);
  592. }
  593. static int link_path_walk(const char *, struct nameidata *);
  594. static __always_inline unsigned set_root_rcu(struct nameidata *nd)
  595. {
  596. struct fs_struct *fs = current->fs;
  597. unsigned seq, res;
  598. do {
  599. seq = read_seqcount_begin(&fs->seq);
  600. nd->root = fs->root;
  601. res = __read_seqcount_begin(&nd->root.dentry->d_seq);
  602. } while (read_seqcount_retry(&fs->seq, seq));
  603. return res;
  604. }
  605. static void path_put_conditional(struct path *path, struct nameidata *nd)
  606. {
  607. dput(path->dentry);
  608. if (path->mnt != nd->path.mnt)
  609. mntput(path->mnt);
  610. }
  611. static inline void path_to_nameidata(const struct path *path,
  612. struct nameidata *nd)
  613. {
  614. if (!(nd->flags & LOOKUP_RCU)) {
  615. dput(nd->path.dentry);
  616. if (nd->path.mnt != path->mnt)
  617. mntput(nd->path.mnt);
  618. }
  619. nd->path.mnt = path->mnt;
  620. nd->path.dentry = path->dentry;
  621. }
  622. /*
  623. * Helper to directly jump to a known parsed path from ->follow_link,
  624. * caller must have taken a reference to path beforehand.
  625. */
  626. void nd_jump_link(struct nameidata *nd, struct path *path)
  627. {
  628. path_put(&nd->path);
  629. nd->path = *path;
  630. nd->inode = nd->path.dentry->d_inode;
  631. nd->flags |= LOOKUP_JUMPED;
  632. }
  633. void nd_set_link(struct nameidata *nd, char *path)
  634. {
  635. nd->saved_names[nd->depth] = path;
  636. }
  637. EXPORT_SYMBOL(nd_set_link);
  638. char *nd_get_link(struct nameidata *nd)
  639. {
  640. return nd->saved_names[nd->depth];
  641. }
  642. EXPORT_SYMBOL(nd_get_link);
  643. static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  644. {
  645. struct inode *inode = link->dentry->d_inode;
  646. if (inode->i_op->put_link)
  647. inode->i_op->put_link(link->dentry, nd, cookie);
  648. path_put(link);
  649. }
  650. int sysctl_protected_symlinks __read_mostly = 0;
  651. int sysctl_protected_hardlinks __read_mostly = 0;
  652. /**
  653. * may_follow_link - Check symlink following for unsafe situations
  654. * @link: The path of the symlink
  655. * @nd: nameidata pathwalk data
  656. *
  657. * In the case of the sysctl_protected_symlinks sysctl being enabled,
  658. * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
  659. * in a sticky world-writable directory. This is to protect privileged
  660. * processes from failing races against path names that may change out
  661. * from under them by way of other users creating malicious symlinks.
  662. * It will permit symlinks to be followed only when outside a sticky
  663. * world-writable directory, or when the uid of the symlink and follower
  664. * match, or when the directory owner matches the symlink's owner.
  665. *
  666. * Returns 0 if following the symlink is allowed, -ve on error.
  667. */
  668. static inline int may_follow_link(struct path *link, struct nameidata *nd)
  669. {
  670. const struct inode *inode;
  671. const struct inode *parent;
  672. if (!sysctl_protected_symlinks)
  673. return 0;
  674. /* Allowed if owner and follower match. */
  675. inode = link->dentry->d_inode;
  676. if (uid_eq(current_cred()->fsuid, inode->i_uid))
  677. return 0;
  678. /* Allowed if parent directory not sticky and world-writable. */
  679. parent = nd->path.dentry->d_inode;
  680. if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
  681. return 0;
  682. /* Allowed if parent directory and link owner match. */
  683. if (uid_eq(parent->i_uid, inode->i_uid))
  684. return 0;
  685. audit_log_link_denied("follow_link", link);
  686. path_put_conditional(link, nd);
  687. path_put(&nd->path);
  688. return -EACCES;
  689. }
  690. /**
  691. * safe_hardlink_source - Check for safe hardlink conditions
  692. * @inode: the source inode to hardlink from
  693. *
  694. * Return false if at least one of the following conditions:
  695. * - inode is not a regular file
  696. * - inode is setuid
  697. * - inode is setgid and group-exec
  698. * - access failure for read and write
  699. *
  700. * Otherwise returns true.
  701. */
  702. static bool safe_hardlink_source(struct inode *inode)
  703. {
  704. umode_t mode = inode->i_mode;
  705. /* Special files should not get pinned to the filesystem. */
  706. if (!S_ISREG(mode))
  707. return false;
  708. /* Setuid files should not get pinned to the filesystem. */
  709. if (mode & S_ISUID)
  710. return false;
  711. /* Executable setgid files should not get pinned to the filesystem. */
  712. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  713. return false;
  714. /* Hardlinking to unreadable or unwritable sources is dangerous. */
  715. if (inode_permission(inode, MAY_READ | MAY_WRITE))
  716. return false;
  717. return true;
  718. }
  719. /**
  720. * may_linkat - Check permissions for creating a hardlink
  721. * @link: the source to hardlink from
  722. *
  723. * Block hardlink when all of:
  724. * - sysctl_protected_hardlinks enabled
  725. * - fsuid does not match inode
  726. * - hardlink source is unsafe (see safe_hardlink_source() above)
  727. * - not CAP_FOWNER
  728. *
  729. * Returns 0 if successful, -ve on error.
  730. */
  731. static int may_linkat(struct path *link)
  732. {
  733. const struct cred *cred;
  734. struct inode *inode;
  735. if (!sysctl_protected_hardlinks)
  736. return 0;
  737. cred = current_cred();
  738. inode = link->dentry->d_inode;
  739. /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
  740. * otherwise, it must be a safe source.
  741. */
  742. if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
  743. capable(CAP_FOWNER))
  744. return 0;
  745. audit_log_link_denied("linkat", link);
  746. return -EPERM;
  747. }
  748. static __always_inline int
  749. follow_link(struct path *link, struct nameidata *nd, void **p)
  750. {
  751. struct dentry *dentry = link->dentry;
  752. int error;
  753. char *s;
  754. BUG_ON(nd->flags & LOOKUP_RCU);
  755. if (link->mnt == nd->path.mnt)
  756. mntget(link->mnt);
  757. error = -ELOOP;
  758. if (unlikely(current->total_link_count >= 40))
  759. goto out_put_nd_path;
  760. cond_resched();
  761. current->total_link_count++;
  762. touch_atime(link);
  763. nd_set_link(nd, NULL);
  764. error = security_inode_follow_link(link->dentry, nd);
  765. if (error)
  766. goto out_put_nd_path;
  767. nd->last_type = LAST_BIND;
  768. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  769. error = PTR_ERR(*p);
  770. if (IS_ERR(*p))
  771. goto out_put_nd_path;
  772. error = 0;
  773. s = nd_get_link(nd);
  774. if (s) {
  775. if (unlikely(IS_ERR(s))) {
  776. path_put(&nd->path);
  777. put_link(nd, link, *p);
  778. return PTR_ERR(s);
  779. }
  780. if (*s == '/') {
  781. if (!nd->root.mnt)
  782. set_root(nd);
  783. path_put(&nd->path);
  784. nd->path = nd->root;
  785. path_get(&nd->root);
  786. nd->flags |= LOOKUP_JUMPED;
  787. }
  788. nd->inode = nd->path.dentry->d_inode;
  789. error = link_path_walk(s, nd);
  790. if (unlikely(error))
  791. put_link(nd, link, *p);
  792. }
  793. return error;
  794. out_put_nd_path:
  795. *p = NULL;
  796. path_put(&nd->path);
  797. path_put(link);
  798. return error;
  799. }
  800. static int follow_up_rcu(struct path *path)
  801. {
  802. struct mount *mnt = real_mount(path->mnt);
  803. struct mount *parent;
  804. struct dentry *mountpoint;
  805. parent = mnt->mnt_parent;
  806. if (&parent->mnt == path->mnt)
  807. return 0;
  808. mountpoint = mnt->mnt_mountpoint;
  809. path->dentry = mountpoint;
  810. path->mnt = &parent->mnt;
  811. return 1;
  812. }
  813. /*
  814. * follow_up - Find the mountpoint of path's vfsmount
  815. *
  816. * Given a path, find the mountpoint of its source file system.
  817. * Replace @path with the path of the mountpoint in the parent mount.
  818. * Up is towards /.
  819. *
  820. * Return 1 if we went up a level and 0 if we were already at the
  821. * root.
  822. */
  823. int follow_up(struct path *path)
  824. {
  825. struct mount *mnt = real_mount(path->mnt);
  826. struct mount *parent;
  827. struct dentry *mountpoint;
  828. read_seqlock_excl(&mount_lock);
  829. parent = mnt->mnt_parent;
  830. if (parent == mnt) {
  831. read_sequnlock_excl(&mount_lock);
  832. return 0;
  833. }
  834. mntget(&parent->mnt);
  835. mountpoint = dget(mnt->mnt_mountpoint);
  836. read_sequnlock_excl(&mount_lock);
  837. dput(path->dentry);
  838. path->dentry = mountpoint;
  839. mntput(path->mnt);
  840. path->mnt = &parent->mnt;
  841. return 1;
  842. }
  843. EXPORT_SYMBOL(follow_up);
  844. /*
  845. * Perform an automount
  846. * - return -EISDIR to tell follow_managed() to stop and return the path we
  847. * were called with.
  848. */
  849. static int follow_automount(struct path *path, unsigned flags,
  850. bool *need_mntput)
  851. {
  852. struct vfsmount *mnt;
  853. int err;
  854. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  855. return -EREMOTE;
  856. /* We don't want to mount if someone's just doing a stat -
  857. * unless they're stat'ing a directory and appended a '/' to
  858. * the name.
  859. *
  860. * We do, however, want to mount if someone wants to open or
  861. * create a file of any type under the mountpoint, wants to
  862. * traverse through the mountpoint or wants to open the
  863. * mounted directory. Also, autofs may mark negative dentries
  864. * as being automount points. These will need the attentions
  865. * of the daemon to instantiate them before they can be used.
  866. */
  867. if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  868. LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
  869. path->dentry->d_inode)
  870. return -EISDIR;
  871. current->total_link_count++;
  872. if (current->total_link_count >= 40)
  873. return -ELOOP;
  874. mnt = path->dentry->d_op->d_automount(path);
  875. if (IS_ERR(mnt)) {
  876. /*
  877. * The filesystem is allowed to return -EISDIR here to indicate
  878. * it doesn't want to automount. For instance, autofs would do
  879. * this so that its userspace daemon can mount on this dentry.
  880. *
  881. * However, we can only permit this if it's a terminal point in
  882. * the path being looked up; if it wasn't then the remainder of
  883. * the path is inaccessible and we should say so.
  884. */
  885. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
  886. return -EREMOTE;
  887. return PTR_ERR(mnt);
  888. }
  889. if (!mnt) /* mount collision */
  890. return 0;
  891. if (!*need_mntput) {
  892. /* lock_mount() may release path->mnt on error */
  893. mntget(path->mnt);
  894. *need_mntput = true;
  895. }
  896. err = finish_automount(mnt, path);
  897. switch (err) {
  898. case -EBUSY:
  899. /* Someone else made a mount here whilst we were busy */
  900. return 0;
  901. case 0:
  902. path_put(path);
  903. path->mnt = mnt;
  904. path->dentry = dget(mnt->mnt_root);
  905. return 0;
  906. default:
  907. return err;
  908. }
  909. }
  910. /*
  911. * Handle a dentry that is managed in some way.
  912. * - Flagged for transit management (autofs)
  913. * - Flagged as mountpoint
  914. * - Flagged as automount point
  915. *
  916. * This may only be called in refwalk mode.
  917. *
  918. * Serialization is taken care of in namespace.c
  919. */
  920. static int follow_managed(struct path *path, unsigned flags)
  921. {
  922. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  923. unsigned managed;
  924. bool need_mntput = false;
  925. int ret = 0;
  926. /* Given that we're not holding a lock here, we retain the value in a
  927. * local variable for each dentry as we look at it so that we don't see
  928. * the components of that value change under us */
  929. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  930. managed &= DCACHE_MANAGED_DENTRY,
  931. unlikely(managed != 0)) {
  932. /* Allow the filesystem to manage the transit without i_mutex
  933. * being held. */
  934. if (managed & DCACHE_MANAGE_TRANSIT) {
  935. BUG_ON(!path->dentry->d_op);
  936. BUG_ON(!path->dentry->d_op->d_manage);
  937. ret = path->dentry->d_op->d_manage(path->dentry, false);
  938. if (ret < 0)
  939. break;
  940. }
  941. /* Transit to a mounted filesystem. */
  942. if (managed & DCACHE_MOUNTED) {
  943. struct vfsmount *mounted = lookup_mnt(path);
  944. if (mounted) {
  945. dput(path->dentry);
  946. if (need_mntput)
  947. mntput(path->mnt);
  948. path->mnt = mounted;
  949. path->dentry = dget(mounted->mnt_root);
  950. need_mntput = true;
  951. continue;
  952. }
  953. /* Something is mounted on this dentry in another
  954. * namespace and/or whatever was mounted there in this
  955. * namespace got unmounted before lookup_mnt() could
  956. * get it */
  957. }
  958. /* Handle an automount point */
  959. if (managed & DCACHE_NEED_AUTOMOUNT) {
  960. ret = follow_automount(path, flags, &need_mntput);
  961. if (ret < 0)
  962. break;
  963. continue;
  964. }
  965. /* We didn't change the current path point */
  966. break;
  967. }
  968. if (need_mntput && path->mnt == mnt)
  969. mntput(path->mnt);
  970. if (ret == -EISDIR)
  971. ret = 0;
  972. return ret < 0 ? ret : need_mntput;
  973. }
  974. int follow_down_one(struct path *path)
  975. {
  976. struct vfsmount *mounted;
  977. mounted = lookup_mnt(path);
  978. if (mounted) {
  979. dput(path->dentry);
  980. mntput(path->mnt);
  981. path->mnt = mounted;
  982. path->dentry = dget(mounted->mnt_root);
  983. return 1;
  984. }
  985. return 0;
  986. }
  987. EXPORT_SYMBOL(follow_down_one);
  988. static inline int managed_dentry_rcu(struct dentry *dentry)
  989. {
  990. return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
  991. dentry->d_op->d_manage(dentry, true) : 0;
  992. }
  993. /*
  994. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  995. * we meet a managed dentry that would need blocking.
  996. */
  997. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  998. struct inode **inode)
  999. {
  1000. for (;;) {
  1001. struct mount *mounted;
  1002. /*
  1003. * Don't forget we might have a non-mountpoint managed dentry
  1004. * that wants to block transit.
  1005. */
  1006. switch (managed_dentry_rcu(path->dentry)) {
  1007. case -ECHILD:
  1008. default:
  1009. return false;
  1010. case -EISDIR:
  1011. return true;
  1012. case 0:
  1013. break;
  1014. }
  1015. if (!d_mountpoint(path->dentry))
  1016. return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1017. mounted = __lookup_mnt(path->mnt, path->dentry);
  1018. if (!mounted)
  1019. break;
  1020. path->mnt = &mounted->mnt;
  1021. path->dentry = mounted->mnt.mnt_root;
  1022. nd->flags |= LOOKUP_JUMPED;
  1023. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  1024. /*
  1025. * Update the inode too. We don't need to re-check the
  1026. * dentry sequence number here after this d_inode read,
  1027. * because a mount-point is always pinned.
  1028. */
  1029. *inode = path->dentry->d_inode;
  1030. }
  1031. return !read_seqretry(&mount_lock, nd->m_seq) &&
  1032. !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
  1033. }
  1034. static int follow_dotdot_rcu(struct nameidata *nd)
  1035. {
  1036. struct inode *inode = nd->inode;
  1037. if (!nd->root.mnt)
  1038. set_root_rcu(nd);
  1039. while (1) {
  1040. if (nd->path.dentry == nd->root.dentry &&
  1041. nd->path.mnt == nd->root.mnt) {
  1042. break;
  1043. }
  1044. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1045. struct dentry *old = nd->path.dentry;
  1046. struct dentry *parent = old->d_parent;
  1047. unsigned seq;
  1048. inode = parent->d_inode;
  1049. seq = read_seqcount_begin(&parent->d_seq);
  1050. if (read_seqcount_retry(&old->d_seq, nd->seq))
  1051. goto failed;
  1052. nd->path.dentry = parent;
  1053. nd->seq = seq;
  1054. break;
  1055. }
  1056. if (!follow_up_rcu(&nd->path))
  1057. break;
  1058. inode = nd->path.dentry->d_inode;
  1059. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1060. }
  1061. while (d_mountpoint(nd->path.dentry)) {
  1062. struct mount *mounted;
  1063. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
  1064. if (!mounted)
  1065. break;
  1066. nd->path.mnt = &mounted->mnt;
  1067. nd->path.dentry = mounted->mnt.mnt_root;
  1068. inode = nd->path.dentry->d_inode;
  1069. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1070. if (read_seqretry(&mount_lock, nd->m_seq))
  1071. goto failed;
  1072. }
  1073. nd->inode = inode;
  1074. return 0;
  1075. failed:
  1076. nd->flags &= ~LOOKUP_RCU;
  1077. if (!(nd->flags & LOOKUP_ROOT))
  1078. nd->root.mnt = NULL;
  1079. rcu_read_unlock();
  1080. return -ECHILD;
  1081. }
  1082. /*
  1083. * Follow down to the covering mount currently visible to userspace. At each
  1084. * point, the filesystem owning that dentry may be queried as to whether the
  1085. * caller is permitted to proceed or not.
  1086. */
  1087. int follow_down(struct path *path)
  1088. {
  1089. unsigned managed;
  1090. int ret;
  1091. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1092. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  1093. /* Allow the filesystem to manage the transit without i_mutex
  1094. * being held.
  1095. *
  1096. * We indicate to the filesystem if someone is trying to mount
  1097. * something here. This gives autofs the chance to deny anyone
  1098. * other than its daemon the right to mount on its
  1099. * superstructure.
  1100. *
  1101. * The filesystem may sleep at this point.
  1102. */
  1103. if (managed & DCACHE_MANAGE_TRANSIT) {
  1104. BUG_ON(!path->dentry->d_op);
  1105. BUG_ON(!path->dentry->d_op->d_manage);
  1106. ret = path->dentry->d_op->d_manage(
  1107. path->dentry, false);
  1108. if (ret < 0)
  1109. return ret == -EISDIR ? 0 : ret;
  1110. }
  1111. /* Transit to a mounted filesystem. */
  1112. if (managed & DCACHE_MOUNTED) {
  1113. struct vfsmount *mounted = lookup_mnt(path);
  1114. if (!mounted)
  1115. break;
  1116. dput(path->dentry);
  1117. mntput(path->mnt);
  1118. path->mnt = mounted;
  1119. path->dentry = dget(mounted->mnt_root);
  1120. continue;
  1121. }
  1122. /* Don't handle automount points here */
  1123. break;
  1124. }
  1125. return 0;
  1126. }
  1127. EXPORT_SYMBOL(follow_down);
  1128. /*
  1129. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1130. */
  1131. static void follow_mount(struct path *path)
  1132. {
  1133. while (d_mountpoint(path->dentry)) {
  1134. struct vfsmount *mounted = lookup_mnt(path);
  1135. if (!mounted)
  1136. break;
  1137. dput(path->dentry);
  1138. mntput(path->mnt);
  1139. path->mnt = mounted;
  1140. path->dentry = dget(mounted->mnt_root);
  1141. }
  1142. }
  1143. static void follow_dotdot(struct nameidata *nd)
  1144. {
  1145. if (!nd->root.mnt)
  1146. set_root(nd);
  1147. while(1) {
  1148. struct dentry *old = nd->path.dentry;
  1149. if (nd->path.dentry == nd->root.dentry &&
  1150. nd->path.mnt == nd->root.mnt) {
  1151. break;
  1152. }
  1153. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1154. /* rare case of legitimate dget_parent()... */
  1155. nd->path.dentry = dget_parent(nd->path.dentry);
  1156. dput(old);
  1157. break;
  1158. }
  1159. if (!follow_up(&nd->path))
  1160. break;
  1161. }
  1162. follow_mount(&nd->path);
  1163. nd->inode = nd->path.dentry->d_inode;
  1164. }
  1165. /*
  1166. * This looks up the name in dcache, possibly revalidates the old dentry and
  1167. * allocates a new one if not found or not valid. In the need_lookup argument
  1168. * returns whether i_op->lookup is necessary.
  1169. *
  1170. * dir->d_inode->i_mutex must be held
  1171. */
  1172. static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
  1173. unsigned int flags, bool *need_lookup)
  1174. {
  1175. struct dentry *dentry;
  1176. int error;
  1177. *need_lookup = false;
  1178. dentry = d_lookup(dir, name);
  1179. if (dentry) {
  1180. if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
  1181. error = d_revalidate(dentry, flags);
  1182. if (unlikely(error <= 0)) {
  1183. if (error < 0) {
  1184. dput(dentry);
  1185. return ERR_PTR(error);
  1186. } else {
  1187. d_invalidate(dentry);
  1188. dput(dentry);
  1189. dentry = NULL;
  1190. }
  1191. }
  1192. }
  1193. }
  1194. if (!dentry) {
  1195. dentry = d_alloc(dir, name);
  1196. if (unlikely(!dentry))
  1197. return ERR_PTR(-ENOMEM);
  1198. *need_lookup = true;
  1199. }
  1200. return dentry;
  1201. }
  1202. /*
  1203. * Call i_op->lookup on the dentry. The dentry must be negative and
  1204. * unhashed.
  1205. *
  1206. * dir->d_inode->i_mutex must be held
  1207. */
  1208. static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
  1209. unsigned int flags)
  1210. {
  1211. struct dentry *old;
  1212. /* Don't create child dentry for a dead directory. */
  1213. if (unlikely(IS_DEADDIR(dir))) {
  1214. dput(dentry);
  1215. return ERR_PTR(-ENOENT);
  1216. }
  1217. old = dir->i_op->lookup(dir, dentry, flags);
  1218. if (unlikely(old)) {
  1219. dput(dentry);
  1220. dentry = old;
  1221. }
  1222. return dentry;
  1223. }
  1224. static struct dentry *__lookup_hash(struct qstr *name,
  1225. struct dentry *base, unsigned int flags)
  1226. {
  1227. bool need_lookup;
  1228. struct dentry *dentry;
  1229. dentry = lookup_dcache(name, base, flags, &need_lookup);
  1230. if (!need_lookup)
  1231. return dentry;
  1232. return lookup_real(base->d_inode, dentry, flags);
  1233. }
  1234. /*
  1235. * It's more convoluted than I'd like it to be, but... it's still fairly
  1236. * small and for now I'd prefer to have fast path as straight as possible.
  1237. * It _is_ time-critical.
  1238. */
  1239. static int lookup_fast(struct nameidata *nd,
  1240. struct path *path, struct inode **inode)
  1241. {
  1242. struct vfsmount *mnt = nd->path.mnt;
  1243. struct dentry *dentry, *parent = nd->path.dentry;
  1244. int need_reval = 1;
  1245. int status = 1;
  1246. int err;
  1247. /*
  1248. * Rename seqlock is not required here because in the off chance
  1249. * of a false negative due to a concurrent rename, we're going to
  1250. * do the non-racy lookup, below.
  1251. */
  1252. if (nd->flags & LOOKUP_RCU) {
  1253. unsigned seq;
  1254. dentry = __d_lookup_rcu(parent, &nd->last, &seq);
  1255. if (!dentry)
  1256. goto unlazy;
  1257. /*
  1258. * This sequence count validates that the inode matches
  1259. * the dentry name information from lookup.
  1260. */
  1261. *inode = dentry->d_inode;
  1262. if (read_seqcount_retry(&dentry->d_seq, seq))
  1263. return -ECHILD;
  1264. /*
  1265. * This sequence count validates that the parent had no
  1266. * changes while we did the lookup of the dentry above.
  1267. *
  1268. * The memory barrier in read_seqcount_begin of child is
  1269. * enough, we can use __read_seqcount_retry here.
  1270. */
  1271. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1272. return -ECHILD;
  1273. nd->seq = seq;
  1274. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1275. status = d_revalidate(dentry, nd->flags);
  1276. if (unlikely(status <= 0)) {
  1277. if (status != -ECHILD)
  1278. need_reval = 0;
  1279. goto unlazy;
  1280. }
  1281. }
  1282. path->mnt = mnt;
  1283. path->dentry = dentry;
  1284. if (likely(__follow_mount_rcu(nd, path, inode)))
  1285. return 0;
  1286. unlazy:
  1287. if (unlazy_walk(nd, dentry))
  1288. return -ECHILD;
  1289. } else {
  1290. dentry = __d_lookup(parent, &nd->last);
  1291. }
  1292. if (unlikely(!dentry))
  1293. goto need_lookup;
  1294. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1295. status = d_revalidate(dentry, nd->flags);
  1296. if (unlikely(status <= 0)) {
  1297. if (status < 0) {
  1298. dput(dentry);
  1299. return status;
  1300. }
  1301. d_invalidate(dentry);
  1302. dput(dentry);
  1303. goto need_lookup;
  1304. }
  1305. path->mnt = mnt;
  1306. path->dentry = dentry;
  1307. err = follow_managed(path, nd->flags);
  1308. if (unlikely(err < 0)) {
  1309. path_put_conditional(path, nd);
  1310. return err;
  1311. }
  1312. if (err)
  1313. nd->flags |= LOOKUP_JUMPED;
  1314. *inode = path->dentry->d_inode;
  1315. return 0;
  1316. need_lookup:
  1317. return 1;
  1318. }
  1319. /* Fast lookup failed, do it the slow way */
  1320. static int lookup_slow(struct nameidata *nd, struct path *path)
  1321. {
  1322. struct dentry *dentry, *parent;
  1323. int err;
  1324. parent = nd->path.dentry;
  1325. BUG_ON(nd->inode != parent->d_inode);
  1326. mutex_lock(&parent->d_inode->i_mutex);
  1327. dentry = __lookup_hash(&nd->last, parent, nd->flags);
  1328. mutex_unlock(&parent->d_inode->i_mutex);
  1329. if (IS_ERR(dentry))
  1330. return PTR_ERR(dentry);
  1331. path->mnt = nd->path.mnt;
  1332. path->dentry = dentry;
  1333. err = follow_managed(path, nd->flags);
  1334. if (unlikely(err < 0)) {
  1335. path_put_conditional(path, nd);
  1336. return err;
  1337. }
  1338. if (err)
  1339. nd->flags |= LOOKUP_JUMPED;
  1340. return 0;
  1341. }
  1342. static inline int may_lookup(struct nameidata *nd)
  1343. {
  1344. if (nd->flags & LOOKUP_RCU) {
  1345. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1346. if (err != -ECHILD)
  1347. return err;
  1348. if (unlazy_walk(nd, NULL))
  1349. return -ECHILD;
  1350. }
  1351. return inode_permission(nd->inode, MAY_EXEC);
  1352. }
  1353. static inline int handle_dots(struct nameidata *nd, int type)
  1354. {
  1355. if (type == LAST_DOTDOT) {
  1356. if (nd->flags & LOOKUP_RCU) {
  1357. if (follow_dotdot_rcu(nd))
  1358. return -ECHILD;
  1359. } else
  1360. follow_dotdot(nd);
  1361. }
  1362. return 0;
  1363. }
  1364. static void terminate_walk(struct nameidata *nd)
  1365. {
  1366. if (!(nd->flags & LOOKUP_RCU)) {
  1367. path_put(&nd->path);
  1368. } else {
  1369. nd->flags &= ~LOOKUP_RCU;
  1370. if (!(nd->flags & LOOKUP_ROOT))
  1371. nd->root.mnt = NULL;
  1372. rcu_read_unlock();
  1373. }
  1374. }
  1375. /*
  1376. * Do we need to follow links? We _really_ want to be able
  1377. * to do this check without having to look at inode->i_op,
  1378. * so we keep a cache of "no, this doesn't need follow_link"
  1379. * for the common case.
  1380. */
  1381. static inline int should_follow_link(struct dentry *dentry, int follow)
  1382. {
  1383. return unlikely(d_is_symlink(dentry)) ? follow : 0;
  1384. }
  1385. static inline int walk_component(struct nameidata *nd, struct path *path,
  1386. int follow)
  1387. {
  1388. struct inode *inode;
  1389. int err;
  1390. /*
  1391. * "." and ".." are special - ".." especially so because it has
  1392. * to be able to know about the current root directory and
  1393. * parent relationships.
  1394. */
  1395. if (unlikely(nd->last_type != LAST_NORM))
  1396. return handle_dots(nd, nd->last_type);
  1397. err = lookup_fast(nd, path, &inode);
  1398. if (unlikely(err)) {
  1399. if (err < 0)
  1400. goto out_err;
  1401. err = lookup_slow(nd, path);
  1402. if (err < 0)
  1403. goto out_err;
  1404. inode = path->dentry->d_inode;
  1405. }
  1406. err = -ENOENT;
  1407. if (!inode || d_is_negative(path->dentry))
  1408. goto out_path_put;
  1409. if (should_follow_link(path->dentry, follow)) {
  1410. if (nd->flags & LOOKUP_RCU) {
  1411. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1412. err = -ECHILD;
  1413. goto out_err;
  1414. }
  1415. }
  1416. BUG_ON(inode != path->dentry->d_inode);
  1417. return 1;
  1418. }
  1419. path_to_nameidata(path, nd);
  1420. nd->inode = inode;
  1421. return 0;
  1422. out_path_put:
  1423. path_to_nameidata(path, nd);
  1424. out_err:
  1425. terminate_walk(nd);
  1426. return err;
  1427. }
  1428. /*
  1429. * This limits recursive symlink follows to 8, while
  1430. * limiting consecutive symlinks to 40.
  1431. *
  1432. * Without that kind of total limit, nasty chains of consecutive
  1433. * symlinks can cause almost arbitrarily long lookups.
  1434. */
  1435. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1436. {
  1437. int res;
  1438. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1439. path_put_conditional(path, nd);
  1440. path_put(&nd->path);
  1441. return -ELOOP;
  1442. }
  1443. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1444. nd->depth++;
  1445. current->link_count++;
  1446. do {
  1447. struct path link = *path;
  1448. void *cookie;
  1449. res = follow_link(&link, nd, &cookie);
  1450. if (res)
  1451. break;
  1452. res = walk_component(nd, path, LOOKUP_FOLLOW);
  1453. put_link(nd, &link, cookie);
  1454. } while (res > 0);
  1455. current->link_count--;
  1456. nd->depth--;
  1457. return res;
  1458. }
  1459. /*
  1460. * We can do the critical dentry name comparison and hashing
  1461. * operations one word at a time, but we are limited to:
  1462. *
  1463. * - Architectures with fast unaligned word accesses. We could
  1464. * do a "get_unaligned()" if this helps and is sufficiently
  1465. * fast.
  1466. *
  1467. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1468. * do not trap on the (extremely unlikely) case of a page
  1469. * crossing operation.
  1470. *
  1471. * - Furthermore, we need an efficient 64-bit compile for the
  1472. * 64-bit case in order to generate the "number of bytes in
  1473. * the final mask". Again, that could be replaced with a
  1474. * efficient population count instruction or similar.
  1475. */
  1476. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1477. #include <asm/word-at-a-time.h>
  1478. #ifdef CONFIG_64BIT
  1479. static inline unsigned int fold_hash(unsigned long hash)
  1480. {
  1481. return hash_64(hash, 32);
  1482. }
  1483. #else /* 32-bit case */
  1484. #define fold_hash(x) (x)
  1485. #endif
  1486. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1487. {
  1488. unsigned long a, mask;
  1489. unsigned long hash = 0;
  1490. for (;;) {
  1491. a = load_unaligned_zeropad(name);
  1492. if (len < sizeof(unsigned long))
  1493. break;
  1494. hash += a;
  1495. hash *= 9;
  1496. name += sizeof(unsigned long);
  1497. len -= sizeof(unsigned long);
  1498. if (!len)
  1499. goto done;
  1500. }
  1501. mask = bytemask_from_count(len);
  1502. hash += mask & a;
  1503. done:
  1504. return fold_hash(hash);
  1505. }
  1506. EXPORT_SYMBOL(full_name_hash);
  1507. /*
  1508. * Calculate the length and hash of the path component, and
  1509. * return the "hash_len" as the result.
  1510. */
  1511. static inline u64 hash_name(const char *name)
  1512. {
  1513. unsigned long a, b, adata, bdata, mask, hash, len;
  1514. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1515. hash = a = 0;
  1516. len = -sizeof(unsigned long);
  1517. do {
  1518. hash = (hash + a) * 9;
  1519. len += sizeof(unsigned long);
  1520. a = load_unaligned_zeropad(name+len);
  1521. b = a ^ REPEAT_BYTE('/');
  1522. } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
  1523. adata = prep_zero_mask(a, adata, &constants);
  1524. bdata = prep_zero_mask(b, bdata, &constants);
  1525. mask = create_zero_mask(adata | bdata);
  1526. hash += a & zero_bytemask(mask);
  1527. len += find_zero(mask);
  1528. return hashlen_create(fold_hash(hash), len);
  1529. }
  1530. #else
  1531. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1532. {
  1533. unsigned long hash = init_name_hash();
  1534. while (len--)
  1535. hash = partial_name_hash(*name++, hash);
  1536. return end_name_hash(hash);
  1537. }
  1538. EXPORT_SYMBOL(full_name_hash);
  1539. /*
  1540. * We know there's a real path component here of at least
  1541. * one character.
  1542. */
  1543. static inline u64 hash_name(const char *name)
  1544. {
  1545. unsigned long hash = init_name_hash();
  1546. unsigned long len = 0, c;
  1547. c = (unsigned char)*name;
  1548. do {
  1549. len++;
  1550. hash = partial_name_hash(c, hash);
  1551. c = (unsigned char)name[len];
  1552. } while (c && c != '/');
  1553. return hashlen_create(end_name_hash(hash), len);
  1554. }
  1555. #endif
  1556. /*
  1557. * Name resolution.
  1558. * This is the basic name resolution function, turning a pathname into
  1559. * the final dentry. We expect 'base' to be positive and a directory.
  1560. *
  1561. * Returns 0 and nd will have valid dentry and mnt on success.
  1562. * Returns error and drops reference to input namei data on failure.
  1563. */
  1564. static int link_path_walk(const char *name, struct nameidata *nd)
  1565. {
  1566. struct path next;
  1567. int err;
  1568. while (*name=='/')
  1569. name++;
  1570. if (!*name)
  1571. return 0;
  1572. /* At this point we know we have a real path component. */
  1573. for(;;) {
  1574. u64 hash_len;
  1575. int type;
  1576. err = may_lookup(nd);
  1577. if (err)
  1578. break;
  1579. hash_len = hash_name(name);
  1580. type = LAST_NORM;
  1581. if (name[0] == '.') switch (hashlen_len(hash_len)) {
  1582. case 2:
  1583. if (name[1] == '.') {
  1584. type = LAST_DOTDOT;
  1585. nd->flags |= LOOKUP_JUMPED;
  1586. }
  1587. break;
  1588. case 1:
  1589. type = LAST_DOT;
  1590. }
  1591. if (likely(type == LAST_NORM)) {
  1592. struct dentry *parent = nd->path.dentry;
  1593. nd->flags &= ~LOOKUP_JUMPED;
  1594. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1595. struct qstr this = { { .hash_len = hash_len }, .name = name };
  1596. err = parent->d_op->d_hash(parent, &this);
  1597. if (err < 0)
  1598. break;
  1599. hash_len = this.hash_len;
  1600. name = this.name;
  1601. }
  1602. }
  1603. nd->last.hash_len = hash_len;
  1604. nd->last.name = name;
  1605. nd->last_type = type;
  1606. name += hashlen_len(hash_len);
  1607. if (!*name)
  1608. return 0;
  1609. /*
  1610. * If it wasn't NUL, we know it was '/'. Skip that
  1611. * slash, and continue until no more slashes.
  1612. */
  1613. do {
  1614. name++;
  1615. } while (unlikely(*name == '/'));
  1616. if (!*name)
  1617. return 0;
  1618. err = walk_component(nd, &next, LOOKUP_FOLLOW);
  1619. if (err < 0)
  1620. return err;
  1621. if (err) {
  1622. err = nested_symlink(&next, nd);
  1623. if (err)
  1624. return err;
  1625. }
  1626. if (!d_can_lookup(nd->path.dentry)) {
  1627. err = -ENOTDIR;
  1628. break;
  1629. }
  1630. }
  1631. terminate_walk(nd);
  1632. return err;
  1633. }
  1634. static int path_init(int dfd, const char *name, unsigned int flags,
  1635. struct nameidata *nd)
  1636. {
  1637. int retval = 0;
  1638. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1639. nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
  1640. nd->depth = 0;
  1641. nd->base = NULL;
  1642. if (flags & LOOKUP_ROOT) {
  1643. struct dentry *root = nd->root.dentry;
  1644. struct inode *inode = root->d_inode;
  1645. if (*name) {
  1646. if (!d_can_lookup(root))
  1647. return -ENOTDIR;
  1648. retval = inode_permission(inode, MAY_EXEC);
  1649. if (retval)
  1650. return retval;
  1651. }
  1652. nd->path = nd->root;
  1653. nd->inode = inode;
  1654. if (flags & LOOKUP_RCU) {
  1655. rcu_read_lock();
  1656. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1657. nd->m_seq = read_seqbegin(&mount_lock);
  1658. } else {
  1659. path_get(&nd->path);
  1660. }
  1661. goto done;
  1662. }
  1663. nd->root.mnt = NULL;
  1664. nd->m_seq = read_seqbegin(&mount_lock);
  1665. if (*name=='/') {
  1666. if (flags & LOOKUP_RCU) {
  1667. rcu_read_lock();
  1668. nd->seq = set_root_rcu(nd);
  1669. } else {
  1670. set_root(nd);
  1671. path_get(&nd->root);
  1672. }
  1673. nd->path = nd->root;
  1674. } else if (dfd == AT_FDCWD) {
  1675. if (flags & LOOKUP_RCU) {
  1676. struct fs_struct *fs = current->fs;
  1677. unsigned seq;
  1678. rcu_read_lock();
  1679. do {
  1680. seq = read_seqcount_begin(&fs->seq);
  1681. nd->path = fs->pwd;
  1682. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1683. } while (read_seqcount_retry(&fs->seq, seq));
  1684. } else {
  1685. get_fs_pwd(current->fs, &nd->path);
  1686. }
  1687. } else {
  1688. /* Caller must check execute permissions on the starting path component */
  1689. struct fd f = fdget_raw(dfd);
  1690. struct dentry *dentry;
  1691. if (!f.file)
  1692. return -EBADF;
  1693. dentry = f.file->f_path.dentry;
  1694. if (*name) {
  1695. if (!d_can_lookup(dentry)) {
  1696. fdput(f);
  1697. return -ENOTDIR;
  1698. }
  1699. }
  1700. nd->path = f.file->f_path;
  1701. if (flags & LOOKUP_RCU) {
  1702. if (f.flags & FDPUT_FPUT)
  1703. nd->base = f.file;
  1704. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1705. rcu_read_lock();
  1706. } else {
  1707. path_get(&nd->path);
  1708. fdput(f);
  1709. }
  1710. }
  1711. nd->inode = nd->path.dentry->d_inode;
  1712. if (!(flags & LOOKUP_RCU))
  1713. goto done;
  1714. if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
  1715. goto done;
  1716. if (!(nd->flags & LOOKUP_ROOT))
  1717. nd->root.mnt = NULL;
  1718. rcu_read_unlock();
  1719. return -ECHILD;
  1720. done:
  1721. current->total_link_count = 0;
  1722. return link_path_walk(name, nd);
  1723. }
  1724. static void path_cleanup(struct nameidata *nd)
  1725. {
  1726. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1727. path_put(&nd->root);
  1728. nd->root.mnt = NULL;
  1729. }
  1730. if (unlikely(nd->base))
  1731. fput(nd->base);
  1732. }
  1733. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1734. {
  1735. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1736. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1737. nd->flags &= ~LOOKUP_PARENT;
  1738. return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
  1739. }
  1740. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1741. static int path_lookupat(int dfd, const char *name,
  1742. unsigned int flags, struct nameidata *nd)
  1743. {
  1744. struct path path;
  1745. int err;
  1746. /*
  1747. * Path walking is largely split up into 2 different synchronisation
  1748. * schemes, rcu-walk and ref-walk (explained in
  1749. * Documentation/filesystems/path-lookup.txt). These share much of the
  1750. * path walk code, but some things particularly setup, cleanup, and
  1751. * following mounts are sufficiently divergent that functions are
  1752. * duplicated. Typically there is a function foo(), and its RCU
  1753. * analogue, foo_rcu().
  1754. *
  1755. * -ECHILD is the error number of choice (just to avoid clashes) that
  1756. * is returned if some aspect of an rcu-walk fails. Such an error must
  1757. * be handled by restarting a traditional ref-walk (which will always
  1758. * be able to complete).
  1759. */
  1760. err = path_init(dfd, name, flags, nd);
  1761. if (!err && !(flags & LOOKUP_PARENT)) {
  1762. err = lookup_last(nd, &path);
  1763. while (err > 0) {
  1764. void *cookie;
  1765. struct path link = path;
  1766. err = may_follow_link(&link, nd);
  1767. if (unlikely(err))
  1768. break;
  1769. nd->flags |= LOOKUP_PARENT;
  1770. err = follow_link(&link, nd, &cookie);
  1771. if (err)
  1772. break;
  1773. err = lookup_last(nd, &path);
  1774. put_link(nd, &link, cookie);
  1775. }
  1776. }
  1777. if (!err)
  1778. err = complete_walk(nd);
  1779. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1780. if (!d_can_lookup(nd->path.dentry)) {
  1781. path_put(&nd->path);
  1782. err = -ENOTDIR;
  1783. }
  1784. }
  1785. path_cleanup(nd);
  1786. return err;
  1787. }
  1788. static int filename_lookup(int dfd, struct filename *name,
  1789. unsigned int flags, struct nameidata *nd)
  1790. {
  1791. int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
  1792. if (unlikely(retval == -ECHILD))
  1793. retval = path_lookupat(dfd, name->name, flags, nd);
  1794. if (unlikely(retval == -ESTALE))
  1795. retval = path_lookupat(dfd, name->name,
  1796. flags | LOOKUP_REVAL, nd);
  1797. if (likely(!retval))
  1798. audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
  1799. return retval;
  1800. }
  1801. static int do_path_lookup(int dfd, const char *name,
  1802. unsigned int flags, struct nameidata *nd)
  1803. {
  1804. struct filename *filename = getname_kernel(name);
  1805. int retval = PTR_ERR(filename);
  1806. if (!IS_ERR(filename)) {
  1807. retval = filename_lookup(dfd, filename, flags, nd);
  1808. putname(filename);
  1809. }
  1810. return retval;
  1811. }
  1812. /* does lookup, returns the object with parent locked */
  1813. struct dentry *kern_path_locked(const char *name, struct path *path)
  1814. {
  1815. struct filename *filename = getname_kernel(name);
  1816. struct nameidata nd;
  1817. struct dentry *d;
  1818. int err;
  1819. if (IS_ERR(filename))
  1820. return ERR_CAST(filename);
  1821. err = filename_lookup(AT_FDCWD, filename, LOOKUP_PARENT, &nd);
  1822. if (err) {
  1823. d = ERR_PTR(err);
  1824. goto out;
  1825. }
  1826. if (nd.last_type != LAST_NORM) {
  1827. path_put(&nd.path);
  1828. d = ERR_PTR(-EINVAL);
  1829. goto out;
  1830. }
  1831. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1832. d = __lookup_hash(&nd.last, nd.path.dentry, 0);
  1833. if (IS_ERR(d)) {
  1834. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1835. path_put(&nd.path);
  1836. goto out;
  1837. }
  1838. *path = nd.path;
  1839. out:
  1840. putname(filename);
  1841. return d;
  1842. }
  1843. int kern_path(const char *name, unsigned int flags, struct path *path)
  1844. {
  1845. struct nameidata nd;
  1846. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1847. if (!res)
  1848. *path = nd.path;
  1849. return res;
  1850. }
  1851. EXPORT_SYMBOL(kern_path);
  1852. /**
  1853. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1854. * @dentry: pointer to dentry of the base directory
  1855. * @mnt: pointer to vfs mount of the base directory
  1856. * @name: pointer to file name
  1857. * @flags: lookup flags
  1858. * @path: pointer to struct path to fill
  1859. */
  1860. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1861. const char *name, unsigned int flags,
  1862. struct path *path)
  1863. {
  1864. struct nameidata nd;
  1865. int err;
  1866. nd.root.dentry = dentry;
  1867. nd.root.mnt = mnt;
  1868. BUG_ON(flags & LOOKUP_PARENT);
  1869. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1870. err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  1871. if (!err)
  1872. *path = nd.path;
  1873. return err;
  1874. }
  1875. EXPORT_SYMBOL(vfs_path_lookup);
  1876. /*
  1877. * Restricted form of lookup. Doesn't follow links, single-component only,
  1878. * needs parent already locked. Doesn't follow mounts.
  1879. * SMP-safe.
  1880. */
  1881. static struct dentry *lookup_hash(struct nameidata *nd)
  1882. {
  1883. return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
  1884. }
  1885. /**
  1886. * lookup_one_len - filesystem helper to lookup single pathname component
  1887. * @name: pathname component to lookup
  1888. * @base: base directory to lookup from
  1889. * @len: maximum length @len should be interpreted to
  1890. *
  1891. * Note that this routine is purely a helper for filesystem usage and should
  1892. * not be called by generic code. Also note that by using this function the
  1893. * nameidata argument is passed to the filesystem methods and a filesystem
  1894. * using this helper needs to be prepared for that.
  1895. */
  1896. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1897. {
  1898. struct qstr this;
  1899. unsigned int c;
  1900. int err;
  1901. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1902. this.name = name;
  1903. this.len = len;
  1904. this.hash = full_name_hash(name, len);
  1905. if (!len)
  1906. return ERR_PTR(-EACCES);
  1907. if (unlikely(name[0] == '.')) {
  1908. if (len < 2 || (len == 2 && name[1] == '.'))
  1909. return ERR_PTR(-EACCES);
  1910. }
  1911. while (len--) {
  1912. c = *(const unsigned char *)name++;
  1913. if (c == '/' || c == '\0')
  1914. return ERR_PTR(-EACCES);
  1915. }
  1916. /*
  1917. * See if the low-level filesystem might want
  1918. * to use its own hash..
  1919. */
  1920. if (base->d_flags & DCACHE_OP_HASH) {
  1921. int err = base->d_op->d_hash(base, &this);
  1922. if (err < 0)
  1923. return ERR_PTR(err);
  1924. }
  1925. err = inode_permission(base->d_inode, MAY_EXEC);
  1926. if (err)
  1927. return ERR_PTR(err);
  1928. return __lookup_hash(&this, base, 0);
  1929. }
  1930. EXPORT_SYMBOL(lookup_one_len);
  1931. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  1932. struct path *path, int *empty)
  1933. {
  1934. struct nameidata nd;
  1935. struct filename *tmp = getname_flags(name, flags, empty);
  1936. int err = PTR_ERR(tmp);
  1937. if (!IS_ERR(tmp)) {
  1938. BUG_ON(flags & LOOKUP_PARENT);
  1939. err = filename_lookup(dfd, tmp, flags, &nd);
  1940. putname(tmp);
  1941. if (!err)
  1942. *path = nd.path;
  1943. }
  1944. return err;
  1945. }
  1946. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1947. struct path *path)
  1948. {
  1949. return user_path_at_empty(dfd, name, flags, path, NULL);
  1950. }
  1951. EXPORT_SYMBOL(user_path_at);
  1952. /*
  1953. * NB: most callers don't do anything directly with the reference to the
  1954. * to struct filename, but the nd->last pointer points into the name string
  1955. * allocated by getname. So we must hold the reference to it until all
  1956. * path-walking is complete.
  1957. */
  1958. static struct filename *
  1959. user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
  1960. unsigned int flags)
  1961. {
  1962. struct filename *s = getname(path);
  1963. int error;
  1964. /* only LOOKUP_REVAL is allowed in extra flags */
  1965. flags &= LOOKUP_REVAL;
  1966. if (IS_ERR(s))
  1967. return s;
  1968. error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
  1969. if (error) {
  1970. putname(s);
  1971. return ERR_PTR(error);
  1972. }
  1973. return s;
  1974. }
  1975. /**
  1976. * mountpoint_last - look up last component for umount
  1977. * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
  1978. * @path: pointer to container for result
  1979. *
  1980. * This is a special lookup_last function just for umount. In this case, we
  1981. * need to resolve the path without doing any revalidation.
  1982. *
  1983. * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
  1984. * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
  1985. * in almost all cases, this lookup will be served out of the dcache. The only
  1986. * cases where it won't are if nd->last refers to a symlink or the path is
  1987. * bogus and it doesn't exist.
  1988. *
  1989. * Returns:
  1990. * -error: if there was an error during lookup. This includes -ENOENT if the
  1991. * lookup found a negative dentry. The nd->path reference will also be
  1992. * put in this case.
  1993. *
  1994. * 0: if we successfully resolved nd->path and found it to not to be a
  1995. * symlink that needs to be followed. "path" will also be populated.
  1996. * The nd->path reference will also be put.
  1997. *
  1998. * 1: if we successfully resolved nd->last and found it to be a symlink
  1999. * that needs to be followed. "path" will be populated with the path
  2000. * to the link, and nd->path will *not* be put.
  2001. */
  2002. static int
  2003. mountpoint_last(struct nameidata *nd, struct path *path)
  2004. {
  2005. int error = 0;
  2006. struct dentry *dentry;
  2007. struct dentry *dir = nd->path.dentry;
  2008. /* If we're in rcuwalk, drop out of it to handle last component */
  2009. if (nd->flags & LOOKUP_RCU) {
  2010. if (unlazy_walk(nd, NULL)) {
  2011. error = -ECHILD;
  2012. goto out;
  2013. }
  2014. }
  2015. nd->flags &= ~LOOKUP_PARENT;
  2016. if (unlikely(nd->last_type != LAST_NORM)) {
  2017. error = handle_dots(nd, nd->last_type);
  2018. if (error)
  2019. goto out;
  2020. dentry = dget(nd->path.dentry);
  2021. goto done;
  2022. }
  2023. mutex_lock(&dir->d_inode->i_mutex);
  2024. dentry = d_lookup(dir, &nd->last);
  2025. if (!dentry) {
  2026. /*
  2027. * No cached dentry. Mounted dentries are pinned in the cache,
  2028. * so that means that this dentry is probably a symlink or the
  2029. * path doesn't actually point to a mounted dentry.
  2030. */
  2031. dentry = d_alloc(dir, &nd->last);
  2032. if (!dentry) {
  2033. error = -ENOMEM;
  2034. mutex_unlock(&dir->d_inode->i_mutex);
  2035. goto out;
  2036. }
  2037. dentry = lookup_real(dir->d_inode, dentry, nd->flags);
  2038. error = PTR_ERR(dentry);
  2039. if (IS_ERR(dentry)) {
  2040. mutex_unlock(&dir->d_inode->i_mutex);
  2041. goto out;
  2042. }
  2043. }
  2044. mutex_unlock(&dir->d_inode->i_mutex);
  2045. done:
  2046. if (!dentry->d_inode || d_is_negative(dentry)) {
  2047. error = -ENOENT;
  2048. dput(dentry);
  2049. goto out;
  2050. }
  2051. path->dentry = dentry;
  2052. path->mnt = nd->path.mnt;
  2053. if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
  2054. return 1;
  2055. mntget(path->mnt);
  2056. follow_mount(path);
  2057. error = 0;
  2058. out:
  2059. terminate_walk(nd);
  2060. return error;
  2061. }
  2062. /**
  2063. * path_mountpoint - look up a path to be umounted
  2064. * @dfd: directory file descriptor to start walk from
  2065. * @name: full pathname to walk
  2066. * @path: pointer to container for result
  2067. * @flags: lookup flags
  2068. *
  2069. * Look up the given name, but don't attempt to revalidate the last component.
  2070. * Returns 0 and "path" will be valid on success; Returns error otherwise.
  2071. */
  2072. static int
  2073. path_mountpoint(int dfd, const char *name, struct path *path, 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 *s, struct path *path,
  2100. unsigned int flags)
  2101. {
  2102. int error;
  2103. if (IS_ERR(s))
  2104. return PTR_ERR(s);
  2105. error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
  2106. if (unlikely(error == -ECHILD))
  2107. error = path_mountpoint(dfd, s->name, path, flags);
  2108. if (unlikely(error == -ESTALE))
  2109. error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
  2110. if (likely(!error))
  2111. audit_inode(s, path->dentry, 0);
  2112. putname(s);
  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 (!inode || 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(unlazy_walk(nd, path->dentry))) {
  2703. error = -ECHILD;
  2704. goto out;
  2705. }
  2706. }
  2707. BUG_ON(inode != path->dentry->d_inode);
  2708. return 1;
  2709. }
  2710. if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
  2711. path_to_nameidata(path, nd);
  2712. } else {
  2713. save_parent.dentry = nd->path.dentry;
  2714. save_parent.mnt = mntget(path->mnt);
  2715. nd->path.dentry = path->dentry;
  2716. }
  2717. nd->inode = inode;
  2718. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2719. finish_open:
  2720. error = complete_walk(nd);
  2721. if (error) {
  2722. path_put(&save_parent);
  2723. return error;
  2724. }
  2725. audit_inode(name, nd->path.dentry, 0);
  2726. error = -EISDIR;
  2727. if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
  2728. goto out;
  2729. error = -ENOTDIR;
  2730. if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
  2731. goto out;
  2732. if (!S_ISREG(nd->inode->i_mode))
  2733. will_truncate = false;
  2734. if (will_truncate) {
  2735. error = mnt_want_write(nd->path.mnt);
  2736. if (error)
  2737. goto out;
  2738. got_write = true;
  2739. }
  2740. finish_open_created:
  2741. error = may_open(&nd->path, acc_mode, open_flag);
  2742. if (error)
  2743. goto out;
  2744. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  2745. error = vfs_open(&nd->path, file, current_cred());
  2746. if (!error) {
  2747. *opened |= FILE_OPENED;
  2748. } else {
  2749. if (error == -EOPENSTALE)
  2750. goto stale_open;
  2751. goto out;
  2752. }
  2753. opened:
  2754. error = open_check_o_direct(file);
  2755. if (error)
  2756. goto exit_fput;
  2757. error = ima_file_check(file, op->acc_mode, *opened);
  2758. if (error)
  2759. goto exit_fput;
  2760. if (will_truncate) {
  2761. error = handle_truncate(file);
  2762. if (error)
  2763. goto exit_fput;
  2764. }
  2765. out:
  2766. if (got_write)
  2767. mnt_drop_write(nd->path.mnt);
  2768. path_put(&save_parent);
  2769. terminate_walk(nd);
  2770. return error;
  2771. exit_dput:
  2772. path_put_conditional(path, nd);
  2773. goto out;
  2774. exit_fput:
  2775. fput(file);
  2776. goto out;
  2777. stale_open:
  2778. /* If no saved parent or already retried then can't retry */
  2779. if (!save_parent.dentry || retried)
  2780. goto out;
  2781. BUG_ON(save_parent.dentry != dir);
  2782. path_put(&nd->path);
  2783. nd->path = save_parent;
  2784. nd->inode = dir->d_inode;
  2785. save_parent.mnt = NULL;
  2786. save_parent.dentry = NULL;
  2787. if (got_write) {
  2788. mnt_drop_write(nd->path.mnt);
  2789. got_write = false;
  2790. }
  2791. retried = true;
  2792. goto retry_lookup;
  2793. }
  2794. static int do_tmpfile(int dfd, struct filename *pathname,
  2795. struct nameidata *nd, int flags,
  2796. const struct open_flags *op,
  2797. struct file *file, int *opened)
  2798. {
  2799. static const struct qstr name = QSTR_INIT("/", 1);
  2800. struct dentry *dentry, *child;
  2801. struct inode *dir;
  2802. int error = path_lookupat(dfd, pathname->name,
  2803. flags | LOOKUP_DIRECTORY, nd);
  2804. if (unlikely(error))
  2805. return error;
  2806. error = mnt_want_write(nd->path.mnt);
  2807. if (unlikely(error))
  2808. goto out;
  2809. /* we want directory to be writable */
  2810. error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
  2811. if (error)
  2812. goto out2;
  2813. dentry = nd->path.dentry;
  2814. dir = dentry->d_inode;
  2815. if (!dir->i_op->tmpfile) {
  2816. error = -EOPNOTSUPP;
  2817. goto out2;
  2818. }
  2819. child = d_alloc(dentry, &name);
  2820. if (unlikely(!child)) {
  2821. error = -ENOMEM;
  2822. goto out2;
  2823. }
  2824. nd->flags &= ~LOOKUP_DIRECTORY;
  2825. nd->flags |= op->intent;
  2826. dput(nd->path.dentry);
  2827. nd->path.dentry = child;
  2828. error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
  2829. if (error)
  2830. goto out2;
  2831. audit_inode(pathname, nd->path.dentry, 0);
  2832. /* Don't check for other permissions, the inode was just created */
  2833. error = may_open(&nd->path, MAY_OPEN, op->open_flag);
  2834. if (error)
  2835. goto out2;
  2836. file->f_path.mnt = nd->path.mnt;
  2837. error = finish_open(file, nd->path.dentry, NULL, opened);
  2838. if (error)
  2839. goto out2;
  2840. error = open_check_o_direct(file);
  2841. if (error) {
  2842. fput(file);
  2843. } else if (!(op->open_flag & O_EXCL)) {
  2844. struct inode *inode = file_inode(file);
  2845. spin_lock(&inode->i_lock);
  2846. inode->i_state |= I_LINKABLE;
  2847. spin_unlock(&inode->i_lock);
  2848. }
  2849. out2:
  2850. mnt_drop_write(nd->path.mnt);
  2851. out:
  2852. path_put(&nd->path);
  2853. return error;
  2854. }
  2855. static struct file *path_openat(int dfd, struct filename *pathname,
  2856. struct nameidata *nd, const struct open_flags *op, int flags)
  2857. {
  2858. struct file *file;
  2859. struct path path;
  2860. int opened = 0;
  2861. int error;
  2862. file = get_empty_filp();
  2863. if (IS_ERR(file))
  2864. return file;
  2865. file->f_flags = op->open_flag;
  2866. if (unlikely(file->f_flags & __O_TMPFILE)) {
  2867. error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
  2868. goto out;
  2869. }
  2870. error = path_init(dfd, pathname->name, flags, nd);
  2871. if (unlikely(error))
  2872. goto out;
  2873. error = do_last(nd, &path, file, op, &opened, pathname);
  2874. while (unlikely(error > 0)) { /* trailing symlink */
  2875. struct path link = path;
  2876. void *cookie;
  2877. if (!(nd->flags & LOOKUP_FOLLOW)) {
  2878. path_put_conditional(&path, nd);
  2879. path_put(&nd->path);
  2880. error = -ELOOP;
  2881. break;
  2882. }
  2883. error = may_follow_link(&link, nd);
  2884. if (unlikely(error))
  2885. break;
  2886. nd->flags |= LOOKUP_PARENT;
  2887. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2888. error = follow_link(&link, nd, &cookie);
  2889. if (unlikely(error))
  2890. break;
  2891. error = do_last(nd, &path, file, op, &opened, pathname);
  2892. put_link(nd, &link, cookie);
  2893. }
  2894. out:
  2895. path_cleanup(nd);
  2896. if (!(opened & FILE_OPENED)) {
  2897. BUG_ON(!error);
  2898. put_filp(file);
  2899. }
  2900. if (unlikely(error)) {
  2901. if (error == -EOPENSTALE) {
  2902. if (flags & LOOKUP_RCU)
  2903. error = -ECHILD;
  2904. else
  2905. error = -ESTALE;
  2906. }
  2907. file = ERR_PTR(error);
  2908. }
  2909. return file;
  2910. }
  2911. struct file *do_filp_open(int dfd, struct filename *pathname,
  2912. const struct open_flags *op)
  2913. {
  2914. struct nameidata nd;
  2915. int flags = op->lookup_flags;
  2916. struct file *filp;
  2917. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2918. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2919. filp = path_openat(dfd, pathname, &nd, op, flags);
  2920. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2921. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2922. return filp;
  2923. }
  2924. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2925. const char *name, const struct open_flags *op)
  2926. {
  2927. struct nameidata nd;
  2928. struct file *file;
  2929. struct filename *filename;
  2930. int flags = op->lookup_flags | LOOKUP_ROOT;
  2931. nd.root.mnt = mnt;
  2932. nd.root.dentry = dentry;
  2933. if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
  2934. return ERR_PTR(-ELOOP);
  2935. filename = getname_kernel(name);
  2936. if (unlikely(IS_ERR(filename)))
  2937. return ERR_CAST(filename);
  2938. file = path_openat(-1, filename, &nd, op, flags | LOOKUP_RCU);
  2939. if (unlikely(file == ERR_PTR(-ECHILD)))
  2940. file = path_openat(-1, filename, &nd, op, flags);
  2941. if (unlikely(file == ERR_PTR(-ESTALE)))
  2942. file = path_openat(-1, filename, &nd, op, flags | LOOKUP_REVAL);
  2943. putname(filename);
  2944. return file;
  2945. }
  2946. static struct dentry *filename_create(int dfd, struct filename *name,
  2947. struct path *path, unsigned int lookup_flags)
  2948. {
  2949. struct dentry *dentry = ERR_PTR(-EEXIST);
  2950. struct nameidata nd;
  2951. int err2;
  2952. int error;
  2953. bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
  2954. /*
  2955. * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
  2956. * other flags passed in are ignored!
  2957. */
  2958. lookup_flags &= LOOKUP_REVAL;
  2959. error = filename_lookup(dfd, name, LOOKUP_PARENT|lookup_flags, &nd);
  2960. if (error)
  2961. return ERR_PTR(error);
  2962. /*
  2963. * Yucky last component or no last component at all?
  2964. * (foo/., foo/.., /////)
  2965. */
  2966. if (nd.last_type != LAST_NORM)
  2967. goto out;
  2968. nd.flags &= ~LOOKUP_PARENT;
  2969. nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2970. /* don't fail immediately if it's r/o, at least try to report other errors */
  2971. err2 = mnt_want_write(nd.path.mnt);
  2972. /*
  2973. * Do the final lookup.
  2974. */
  2975. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2976. dentry = lookup_hash(&nd);
  2977. if (IS_ERR(dentry))
  2978. goto unlock;
  2979. error = -EEXIST;
  2980. if (d_is_positive(dentry))
  2981. goto fail;
  2982. /*
  2983. * Special case - lookup gave negative, but... we had foo/bar/
  2984. * From the vfs_mknod() POV we just have a negative dentry -
  2985. * all is fine. Let's be bastards - you had / on the end, you've
  2986. * been asking for (non-existent) directory. -ENOENT for you.
  2987. */
  2988. if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
  2989. error = -ENOENT;
  2990. goto fail;
  2991. }
  2992. if (unlikely(err2)) {
  2993. error = err2;
  2994. goto fail;
  2995. }
  2996. *path = nd.path;
  2997. return dentry;
  2998. fail:
  2999. dput(dentry);
  3000. dentry = ERR_PTR(error);
  3001. unlock:
  3002. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  3003. if (!err2)
  3004. mnt_drop_write(nd.path.mnt);
  3005. out:
  3006. path_put(&nd.path);
  3007. return dentry;
  3008. }
  3009. struct dentry *kern_path_create(int dfd, const char *pathname,
  3010. struct path *path, unsigned int lookup_flags)
  3011. {
  3012. struct filename *filename = getname_kernel(pathname);
  3013. struct dentry *res;
  3014. if (IS_ERR(filename))
  3015. return ERR_CAST(filename);
  3016. res = filename_create(dfd, filename, path, lookup_flags);
  3017. putname(filename);
  3018. return res;
  3019. }
  3020. EXPORT_SYMBOL(kern_path_create);
  3021. void done_path_create(struct path *path, struct dentry *dentry)
  3022. {
  3023. dput(dentry);
  3024. mutex_unlock(&path->dentry->d_inode->i_mutex);
  3025. mnt_drop_write(path->mnt);
  3026. path_put(path);
  3027. }
  3028. EXPORT_SYMBOL(done_path_create);
  3029. struct dentry *user_path_create(int dfd, const char __user *pathname,
  3030. struct path *path, unsigned int lookup_flags)
  3031. {
  3032. struct filename *tmp = getname(pathname);
  3033. struct dentry *res;
  3034. if (IS_ERR(tmp))
  3035. return ERR_CAST(tmp);
  3036. res = filename_create(dfd, tmp, path, lookup_flags);
  3037. putname(tmp);
  3038. return res;
  3039. }
  3040. EXPORT_SYMBOL(user_path_create);
  3041. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  3042. {
  3043. int error = may_create(dir, dentry);
  3044. if (error)
  3045. return error;
  3046. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  3047. return -EPERM;
  3048. if (!dir->i_op->mknod)
  3049. return -EPERM;
  3050. error = devcgroup_inode_mknod(mode, dev);
  3051. if (error)
  3052. return error;
  3053. error = security_inode_mknod(dir, dentry, mode, dev);
  3054. if (error)
  3055. return error;
  3056. error = dir->i_op->mknod(dir, dentry, mode, dev);
  3057. if (!error)
  3058. fsnotify_create(dir, dentry);
  3059. return error;
  3060. }
  3061. EXPORT_SYMBOL(vfs_mknod);
  3062. static int may_mknod(umode_t mode)
  3063. {
  3064. switch (mode & S_IFMT) {
  3065. case S_IFREG:
  3066. case S_IFCHR:
  3067. case S_IFBLK:
  3068. case S_IFIFO:
  3069. case S_IFSOCK:
  3070. case 0: /* zero mode translates to S_IFREG */
  3071. return 0;
  3072. case S_IFDIR:
  3073. return -EPERM;
  3074. default:
  3075. return -EINVAL;
  3076. }
  3077. }
  3078. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  3079. unsigned, dev)
  3080. {
  3081. struct dentry *dentry;
  3082. struct path path;
  3083. int error;
  3084. unsigned int lookup_flags = 0;
  3085. error = may_mknod(mode);
  3086. if (error)
  3087. return error;
  3088. retry:
  3089. dentry = user_path_create(dfd, filename, &path, lookup_flags);
  3090. if (IS_ERR(dentry))
  3091. return PTR_ERR(dentry);
  3092. if (!IS_POSIXACL(path.dentry->d_inode))
  3093. mode &= ~current_umask();
  3094. error = security_path_mknod(&path, dentry, mode, dev);
  3095. if (error)
  3096. goto out;
  3097. switch (mode & S_IFMT) {
  3098. case 0: case S_IFREG:
  3099. error = vfs_create(path.dentry->d_inode,dentry,mode,true);
  3100. break;
  3101. case S_IFCHR: case S_IFBLK:
  3102. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  3103. new_decode_dev(dev));
  3104. break;
  3105. case S_IFIFO: case S_IFSOCK:
  3106. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  3107. break;
  3108. }
  3109. out:
  3110. done_path_create(&path, dentry);
  3111. if (retry_estale(error, lookup_flags)) {
  3112. lookup_flags |= LOOKUP_REVAL;
  3113. goto retry;
  3114. }
  3115. return error;
  3116. }
  3117. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  3118. {
  3119. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  3120. }
  3121. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3122. {
  3123. int error = may_create(dir, dentry);
  3124. unsigned max_links = dir->i_sb->s_max_links;
  3125. if (error)
  3126. return error;
  3127. if (!dir->i_op->mkdir)
  3128. return -EPERM;
  3129. mode &= (S_IRWXUGO|S_ISVTX);
  3130. error = security_inode_mkdir(dir, dentry, mode);
  3131. if (error)
  3132. return error;
  3133. if (max_links && dir->i_nlink >= max_links)
  3134. return -EMLINK;
  3135. error = dir->i_op->mkdir(dir, dentry, mode);
  3136. if (!error)
  3137. fsnotify_mkdir(dir, dentry);
  3138. return error;
  3139. }
  3140. EXPORT_SYMBOL(vfs_mkdir);
  3141. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  3142. {
  3143. struct dentry *dentry;
  3144. struct path path;
  3145. int error;
  3146. unsigned int lookup_flags = LOOKUP_DIRECTORY;
  3147. retry:
  3148. dentry = user_path_create(dfd, pathname, &path, lookup_flags);
  3149. if (IS_ERR(dentry))
  3150. return PTR_ERR(dentry);
  3151. if (!IS_POSIXACL(path.dentry->d_inode))
  3152. mode &= ~current_umask();
  3153. error = security_path_mkdir(&path, dentry, mode);
  3154. if (!error)
  3155. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  3156. done_path_create(&path, dentry);
  3157. if (retry_estale(error, lookup_flags)) {
  3158. lookup_flags |= LOOKUP_REVAL;
  3159. goto retry;
  3160. }
  3161. return error;
  3162. }
  3163. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  3164. {
  3165. return sys_mkdirat(AT_FDCWD, pathname, mode);
  3166. }
  3167. /*
  3168. * The dentry_unhash() helper will try to drop the dentry early: we
  3169. * should have a usage count of 1 if we're the only user of this
  3170. * dentry, and if that is true (possibly after pruning the dcache),
  3171. * then we drop the dentry now.
  3172. *
  3173. * A low-level filesystem can, if it choses, legally
  3174. * do a
  3175. *
  3176. * if (!d_unhashed(dentry))
  3177. * return -EBUSY;
  3178. *
  3179. * if it cannot handle the case of removing a directory
  3180. * that is still in use by something else..
  3181. */
  3182. void dentry_unhash(struct dentry *dentry)
  3183. {
  3184. shrink_dcache_parent(dentry);
  3185. spin_lock(&dentry->d_lock);
  3186. if (dentry->d_lockref.count == 1)
  3187. __d_drop(dentry);
  3188. spin_unlock(&dentry->d_lock);
  3189. }
  3190. EXPORT_SYMBOL(dentry_unhash);
  3191. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  3192. {
  3193. int error = may_delete(dir, dentry, 1);
  3194. if (error)
  3195. return error;
  3196. if (!dir->i_op->rmdir)
  3197. return -EPERM;
  3198. dget(dentry);
  3199. mutex_lock(&dentry->d_inode->i_mutex);
  3200. error = -EBUSY;
  3201. if (is_local_mountpoint(dentry))
  3202. goto out;
  3203. error = security_inode_rmdir(dir, dentry);
  3204. if (error)
  3205. goto out;
  3206. shrink_dcache_parent(dentry);
  3207. error = dir->i_op->rmdir(dir, dentry);
  3208. if (error)
  3209. goto out;
  3210. dentry->d_inode->i_flags |= S_DEAD;
  3211. dont_mount(dentry);
  3212. detach_mounts(dentry);
  3213. out:
  3214. mutex_unlock(&dentry->d_inode->i_mutex);
  3215. dput(dentry);
  3216. if (!error)
  3217. d_delete(dentry);
  3218. return error;
  3219. }
  3220. EXPORT_SYMBOL(vfs_rmdir);
  3221. static long do_rmdir(int dfd, const char __user *pathname)
  3222. {
  3223. int error = 0;
  3224. struct filename *name;
  3225. struct dentry *dentry;
  3226. struct nameidata nd;
  3227. unsigned int lookup_flags = 0;
  3228. retry:
  3229. name = user_path_parent(dfd, pathname, &nd, lookup_flags);
  3230. if (IS_ERR(name))
  3231. return PTR_ERR(name);
  3232. switch(nd.last_type) {
  3233. case LAST_DOTDOT:
  3234. error = -ENOTEMPTY;
  3235. goto exit1;
  3236. case LAST_DOT:
  3237. error = -EINVAL;
  3238. goto exit1;
  3239. case LAST_ROOT:
  3240. error = -EBUSY;
  3241. goto exit1;
  3242. }
  3243. nd.flags &= ~LOOKUP_PARENT;
  3244. error = mnt_want_write(nd.path.mnt);
  3245. if (error)
  3246. goto exit1;
  3247. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3248. dentry = lookup_hash(&nd);
  3249. error = PTR_ERR(dentry);
  3250. if (IS_ERR(dentry))
  3251. goto exit2;
  3252. if (!dentry->d_inode) {
  3253. error = -ENOENT;
  3254. goto exit3;
  3255. }
  3256. error = security_path_rmdir(&nd.path, dentry);
  3257. if (error)
  3258. goto exit3;
  3259. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  3260. exit3:
  3261. dput(dentry);
  3262. exit2:
  3263. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  3264. mnt_drop_write(nd.path.mnt);
  3265. exit1:
  3266. path_put(&nd.path);
  3267. putname(name);
  3268. if (retry_estale(error, lookup_flags)) {
  3269. lookup_flags |= LOOKUP_REVAL;
  3270. goto retry;
  3271. }
  3272. return error;
  3273. }
  3274. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  3275. {
  3276. return do_rmdir(AT_FDCWD, pathname);
  3277. }
  3278. /**
  3279. * vfs_unlink - unlink a filesystem object
  3280. * @dir: parent directory
  3281. * @dentry: victim
  3282. * @delegated_inode: returns victim inode, if the inode is delegated.
  3283. *
  3284. * The caller must hold dir->i_mutex.
  3285. *
  3286. * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
  3287. * return a reference to the inode in delegated_inode. The caller
  3288. * should then break the delegation on that inode and retry. Because
  3289. * breaking a delegation may take a long time, the caller should drop
  3290. * dir->i_mutex before doing so.
  3291. *
  3292. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3293. * be appropriate for callers that expect the underlying filesystem not
  3294. * to be NFS exported.
  3295. */
  3296. int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
  3297. {
  3298. struct inode *target = dentry->d_inode;
  3299. int error = may_delete(dir, dentry, 0);
  3300. if (error)
  3301. return error;
  3302. if (!dir->i_op->unlink)
  3303. return -EPERM;
  3304. mutex_lock(&target->i_mutex);
  3305. if (is_local_mountpoint(dentry))
  3306. error = -EBUSY;
  3307. else {
  3308. error = security_inode_unlink(dir, dentry);
  3309. if (!error) {
  3310. error = try_break_deleg(target, delegated_inode);
  3311. if (error)
  3312. goto out;
  3313. error = dir->i_op->unlink(dir, dentry);
  3314. if (!error) {
  3315. dont_mount(dentry);
  3316. detach_mounts(dentry);
  3317. }
  3318. }
  3319. }
  3320. out:
  3321. mutex_unlock(&target->i_mutex);
  3322. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  3323. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  3324. fsnotify_link_count(target);
  3325. d_delete(dentry);
  3326. }
  3327. return error;
  3328. }
  3329. EXPORT_SYMBOL(vfs_unlink);
  3330. /*
  3331. * Make sure that the actual truncation of the file will occur outside its
  3332. * directory's i_mutex. Truncate can take a long time if there is a lot of
  3333. * writeout happening, and we don't want to prevent access to the directory
  3334. * while waiting on the I/O.
  3335. */
  3336. static long do_unlinkat(int dfd, const char __user *pathname)
  3337. {
  3338. int error;
  3339. struct filename *name;
  3340. struct dentry *dentry;
  3341. struct nameidata nd;
  3342. struct inode *inode = NULL;
  3343. struct inode *delegated_inode = NULL;
  3344. unsigned int lookup_flags = 0;
  3345. retry:
  3346. name = user_path_parent(dfd, pathname, &nd, lookup_flags);
  3347. if (IS_ERR(name))
  3348. return PTR_ERR(name);
  3349. error = -EISDIR;
  3350. if (nd.last_type != LAST_NORM)
  3351. goto exit1;
  3352. nd.flags &= ~LOOKUP_PARENT;
  3353. error = mnt_want_write(nd.path.mnt);
  3354. if (error)
  3355. goto exit1;
  3356. retry_deleg:
  3357. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3358. dentry = lookup_hash(&nd);
  3359. error = PTR_ERR(dentry);
  3360. if (!IS_ERR(dentry)) {
  3361. /* Why not before? Because we want correct error value */
  3362. if (nd.last.name[nd.last.len])
  3363. goto slashes;
  3364. inode = dentry->d_inode;
  3365. if (d_is_negative(dentry))
  3366. goto slashes;
  3367. ihold(inode);
  3368. error = security_path_unlink(&nd.path, dentry);
  3369. if (error)
  3370. goto exit2;
  3371. error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
  3372. exit2:
  3373. dput(dentry);
  3374. }
  3375. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  3376. if (inode)
  3377. iput(inode); /* truncate the inode here */
  3378. inode = NULL;
  3379. if (delegated_inode) {
  3380. error = break_deleg_wait(&delegated_inode);
  3381. if (!error)
  3382. goto retry_deleg;
  3383. }
  3384. mnt_drop_write(nd.path.mnt);
  3385. exit1:
  3386. path_put(&nd.path);
  3387. putname(name);
  3388. if (retry_estale(error, lookup_flags)) {
  3389. lookup_flags |= LOOKUP_REVAL;
  3390. inode = NULL;
  3391. goto retry;
  3392. }
  3393. return error;
  3394. slashes:
  3395. if (d_is_negative(dentry))
  3396. error = -ENOENT;
  3397. else if (d_is_dir(dentry))
  3398. error = -EISDIR;
  3399. else
  3400. error = -ENOTDIR;
  3401. goto exit2;
  3402. }
  3403. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  3404. {
  3405. if ((flag & ~AT_REMOVEDIR) != 0)
  3406. return -EINVAL;
  3407. if (flag & AT_REMOVEDIR)
  3408. return do_rmdir(dfd, pathname);
  3409. return do_unlinkat(dfd, pathname);
  3410. }
  3411. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  3412. {
  3413. return do_unlinkat(AT_FDCWD, pathname);
  3414. }
  3415. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  3416. {
  3417. int error = may_create(dir, dentry);
  3418. if (error)
  3419. return error;
  3420. if (!dir->i_op->symlink)
  3421. return -EPERM;
  3422. error = security_inode_symlink(dir, dentry, oldname);
  3423. if (error)
  3424. return error;
  3425. error = dir->i_op->symlink(dir, dentry, oldname);
  3426. if (!error)
  3427. fsnotify_create(dir, dentry);
  3428. return error;
  3429. }
  3430. EXPORT_SYMBOL(vfs_symlink);
  3431. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  3432. int, newdfd, const char __user *, newname)
  3433. {
  3434. int error;
  3435. struct filename *from;
  3436. struct dentry *dentry;
  3437. struct path path;
  3438. unsigned int lookup_flags = 0;
  3439. from = getname(oldname);
  3440. if (IS_ERR(from))
  3441. return PTR_ERR(from);
  3442. retry:
  3443. dentry = user_path_create(newdfd, newname, &path, lookup_flags);
  3444. error = PTR_ERR(dentry);
  3445. if (IS_ERR(dentry))
  3446. goto out_putname;
  3447. error = security_path_symlink(&path, dentry, from->name);
  3448. if (!error)
  3449. error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
  3450. done_path_create(&path, dentry);
  3451. if (retry_estale(error, lookup_flags)) {
  3452. lookup_flags |= LOOKUP_REVAL;
  3453. goto retry;
  3454. }
  3455. out_putname:
  3456. putname(from);
  3457. return error;
  3458. }
  3459. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  3460. {
  3461. return sys_symlinkat(oldname, AT_FDCWD, newname);
  3462. }
  3463. /**
  3464. * vfs_link - create a new link
  3465. * @old_dentry: object to be linked
  3466. * @dir: new parent
  3467. * @new_dentry: where to create the new link
  3468. * @delegated_inode: returns inode needing a delegation break
  3469. *
  3470. * The caller must hold dir->i_mutex
  3471. *
  3472. * If vfs_link discovers a delegation on the to-be-linked file in need
  3473. * of breaking, it will return -EWOULDBLOCK and return a reference to the
  3474. * inode in delegated_inode. The caller should then break the delegation
  3475. * and retry. Because breaking a delegation may take a long time, the
  3476. * caller should drop the i_mutex before doing so.
  3477. *
  3478. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3479. * be appropriate for callers that expect the underlying filesystem not
  3480. * to be NFS exported.
  3481. */
  3482. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
  3483. {
  3484. struct inode *inode = old_dentry->d_inode;
  3485. unsigned max_links = dir->i_sb->s_max_links;
  3486. int error;
  3487. if (!inode)
  3488. return -ENOENT;
  3489. error = may_create(dir, new_dentry);
  3490. if (error)
  3491. return error;
  3492. if (dir->i_sb != inode->i_sb)
  3493. return -EXDEV;
  3494. /*
  3495. * A link to an append-only or immutable file cannot be created.
  3496. */
  3497. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3498. return -EPERM;
  3499. if (!dir->i_op->link)
  3500. return -EPERM;
  3501. if (S_ISDIR(inode->i_mode))
  3502. return -EPERM;
  3503. error = security_inode_link(old_dentry, dir, new_dentry);
  3504. if (error)
  3505. return error;
  3506. mutex_lock(&inode->i_mutex);
  3507. /* Make sure we don't allow creating hardlink to an unlinked file */
  3508. if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
  3509. error = -ENOENT;
  3510. else if (max_links && inode->i_nlink >= max_links)
  3511. error = -EMLINK;
  3512. else {
  3513. error = try_break_deleg(inode, delegated_inode);
  3514. if (!error)
  3515. error = dir->i_op->link(old_dentry, dir, new_dentry);
  3516. }
  3517. if (!error && (inode->i_state & I_LINKABLE)) {
  3518. spin_lock(&inode->i_lock);
  3519. inode->i_state &= ~I_LINKABLE;
  3520. spin_unlock(&inode->i_lock);
  3521. }
  3522. mutex_unlock(&inode->i_mutex);
  3523. if (!error)
  3524. fsnotify_link(dir, inode, new_dentry);
  3525. return error;
  3526. }
  3527. EXPORT_SYMBOL(vfs_link);
  3528. /*
  3529. * Hardlinks are often used in delicate situations. We avoid
  3530. * security-related surprises by not following symlinks on the
  3531. * newname. --KAB
  3532. *
  3533. * We don't follow them on the oldname either to be compatible
  3534. * with linux 2.0, and to avoid hard-linking to directories
  3535. * and other special files. --ADM
  3536. */
  3537. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  3538. int, newdfd, const char __user *, newname, int, flags)
  3539. {
  3540. struct dentry *new_dentry;
  3541. struct path old_path, new_path;
  3542. struct inode *delegated_inode = NULL;
  3543. int how = 0;
  3544. int error;
  3545. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  3546. return -EINVAL;
  3547. /*
  3548. * To use null names we require CAP_DAC_READ_SEARCH
  3549. * This ensures that not everyone will be able to create
  3550. * handlink using the passed filedescriptor.
  3551. */
  3552. if (flags & AT_EMPTY_PATH) {
  3553. if (!capable(CAP_DAC_READ_SEARCH))
  3554. return -ENOENT;
  3555. how = LOOKUP_EMPTY;
  3556. }
  3557. if (flags & AT_SYMLINK_FOLLOW)
  3558. how |= LOOKUP_FOLLOW;
  3559. retry:
  3560. error = user_path_at(olddfd, oldname, how, &old_path);
  3561. if (error)
  3562. return error;
  3563. new_dentry = user_path_create(newdfd, newname, &new_path,
  3564. (how & LOOKUP_REVAL));
  3565. error = PTR_ERR(new_dentry);
  3566. if (IS_ERR(new_dentry))
  3567. goto out;
  3568. error = -EXDEV;
  3569. if (old_path.mnt != new_path.mnt)
  3570. goto out_dput;
  3571. error = may_linkat(&old_path);
  3572. if (unlikely(error))
  3573. goto out_dput;
  3574. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  3575. if (error)
  3576. goto out_dput;
  3577. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
  3578. out_dput:
  3579. done_path_create(&new_path, new_dentry);
  3580. if (delegated_inode) {
  3581. error = break_deleg_wait(&delegated_inode);
  3582. if (!error) {
  3583. path_put(&old_path);
  3584. goto retry;
  3585. }
  3586. }
  3587. if (retry_estale(error, how)) {
  3588. path_put(&old_path);
  3589. how |= LOOKUP_REVAL;
  3590. goto retry;
  3591. }
  3592. out:
  3593. path_put(&old_path);
  3594. return error;
  3595. }
  3596. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  3597. {
  3598. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3599. }
  3600. /**
  3601. * vfs_rename - rename a filesystem object
  3602. * @old_dir: parent of source
  3603. * @old_dentry: source
  3604. * @new_dir: parent of destination
  3605. * @new_dentry: destination
  3606. * @delegated_inode: returns an inode needing a delegation break
  3607. * @flags: rename flags
  3608. *
  3609. * The caller must hold multiple mutexes--see lock_rename()).
  3610. *
  3611. * If vfs_rename discovers a delegation in need of breaking at either
  3612. * the source or destination, it will return -EWOULDBLOCK and return a
  3613. * reference to the inode in delegated_inode. The caller should then
  3614. * break the delegation and retry. Because breaking a delegation may
  3615. * take a long time, the caller should drop all locks before doing
  3616. * so.
  3617. *
  3618. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3619. * be appropriate for callers that expect the underlying filesystem not
  3620. * to be NFS exported.
  3621. *
  3622. * The worst of all namespace operations - renaming directory. "Perverted"
  3623. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  3624. * Problems:
  3625. * a) we can get into loop creation.
  3626. * b) race potential - two innocent renames can create a loop together.
  3627. * That's where 4.4 screws up. Current fix: serialization on
  3628. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  3629. * story.
  3630. * c) we have to lock _four_ objects - parents and victim (if it exists),
  3631. * and source (if it is not a directory).
  3632. * And that - after we got ->i_mutex on parents (until then we don't know
  3633. * whether the target exists). Solution: try to be smart with locking
  3634. * order for inodes. We rely on the fact that tree topology may change
  3635. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  3636. * move will be locked. Thus we can rank directories by the tree
  3637. * (ancestors first) and rank all non-directories after them.
  3638. * That works since everybody except rename does "lock parent, lookup,
  3639. * lock child" and rename is under ->s_vfs_rename_mutex.
  3640. * HOWEVER, it relies on the assumption that any object with ->lookup()
  3641. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  3642. * we'd better make sure that there's no link(2) for them.
  3643. * d) conversion from fhandle to dentry may come in the wrong moment - when
  3644. * we are removing the target. Solution: we will have to grab ->i_mutex
  3645. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  3646. * ->i_mutex on parents, which works but leads to some truly excessive
  3647. * locking].
  3648. */
  3649. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  3650. struct inode *new_dir, struct dentry *new_dentry,
  3651. struct inode **delegated_inode, unsigned int flags)
  3652. {
  3653. int error;
  3654. bool is_dir = d_is_dir(old_dentry);
  3655. const unsigned char *old_name;
  3656. struct inode *source = old_dentry->d_inode;
  3657. struct inode *target = new_dentry->d_inode;
  3658. bool new_is_dir = false;
  3659. unsigned max_links = new_dir->i_sb->s_max_links;
  3660. if (source == target)
  3661. return 0;
  3662. error = may_delete(old_dir, old_dentry, is_dir);
  3663. if (error)
  3664. return error;
  3665. if (!target) {
  3666. error = may_create(new_dir, new_dentry);
  3667. } else {
  3668. new_is_dir = d_is_dir(new_dentry);
  3669. if (!(flags & RENAME_EXCHANGE))
  3670. error = may_delete(new_dir, new_dentry, is_dir);
  3671. else
  3672. error = may_delete(new_dir, new_dentry, new_is_dir);
  3673. }
  3674. if (error)
  3675. return error;
  3676. if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
  3677. return -EPERM;
  3678. if (flags && !old_dir->i_op->rename2)
  3679. return -EINVAL;
  3680. /*
  3681. * If we are going to change the parent - check write permissions,
  3682. * we'll need to flip '..'.
  3683. */
  3684. if (new_dir != old_dir) {
  3685. if (is_dir) {
  3686. error = inode_permission(source, MAY_WRITE);
  3687. if (error)
  3688. return error;
  3689. }
  3690. if ((flags & RENAME_EXCHANGE) && new_is_dir) {
  3691. error = inode_permission(target, MAY_WRITE);
  3692. if (error)
  3693. return error;
  3694. }
  3695. }
  3696. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
  3697. flags);
  3698. if (error)
  3699. return error;
  3700. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  3701. dget(new_dentry);
  3702. if (!is_dir || (flags & RENAME_EXCHANGE))
  3703. lock_two_nondirectories(source, target);
  3704. else if (target)
  3705. mutex_lock(&target->i_mutex);
  3706. error = -EBUSY;
  3707. if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
  3708. goto out;
  3709. if (max_links && new_dir != old_dir) {
  3710. error = -EMLINK;
  3711. if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
  3712. goto out;
  3713. if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
  3714. old_dir->i_nlink >= max_links)
  3715. goto out;
  3716. }
  3717. if (is_dir && !(flags & RENAME_EXCHANGE) && target)
  3718. shrink_dcache_parent(new_dentry);
  3719. if (!is_dir) {
  3720. error = try_break_deleg(source, delegated_inode);
  3721. if (error)
  3722. goto out;
  3723. }
  3724. if (target && !new_is_dir) {
  3725. error = try_break_deleg(target, delegated_inode);
  3726. if (error)
  3727. goto out;
  3728. }
  3729. if (!old_dir->i_op->rename2) {
  3730. error = old_dir->i_op->rename(old_dir, old_dentry,
  3731. new_dir, new_dentry);
  3732. } else {
  3733. WARN_ON(old_dir->i_op->rename != NULL);
  3734. error = old_dir->i_op->rename2(old_dir, old_dentry,
  3735. new_dir, new_dentry, flags);
  3736. }
  3737. if (error)
  3738. goto out;
  3739. if (!(flags & RENAME_EXCHANGE) && target) {
  3740. if (is_dir)
  3741. target->i_flags |= S_DEAD;
  3742. dont_mount(new_dentry);
  3743. detach_mounts(new_dentry);
  3744. }
  3745. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
  3746. if (!(flags & RENAME_EXCHANGE))
  3747. d_move(old_dentry, new_dentry);
  3748. else
  3749. d_exchange(old_dentry, new_dentry);
  3750. }
  3751. out:
  3752. if (!is_dir || (flags & RENAME_EXCHANGE))
  3753. unlock_two_nondirectories(source, target);
  3754. else if (target)
  3755. mutex_unlock(&target->i_mutex);
  3756. dput(new_dentry);
  3757. if (!error) {
  3758. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  3759. !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
  3760. if (flags & RENAME_EXCHANGE) {
  3761. fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
  3762. new_is_dir, NULL, new_dentry);
  3763. }
  3764. }
  3765. fsnotify_oldname_free(old_name);
  3766. return error;
  3767. }
  3768. EXPORT_SYMBOL(vfs_rename);
  3769. SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
  3770. int, newdfd, const char __user *, newname, unsigned int, flags)
  3771. {
  3772. struct dentry *old_dir, *new_dir;
  3773. struct dentry *old_dentry, *new_dentry;
  3774. struct dentry *trap;
  3775. struct nameidata oldnd, newnd;
  3776. struct inode *delegated_inode = NULL;
  3777. struct filename *from;
  3778. struct filename *to;
  3779. unsigned int lookup_flags = 0;
  3780. bool should_retry = false;
  3781. int error;
  3782. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
  3783. return -EINVAL;
  3784. if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
  3785. (flags & RENAME_EXCHANGE))
  3786. return -EINVAL;
  3787. if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
  3788. return -EPERM;
  3789. retry:
  3790. from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
  3791. if (IS_ERR(from)) {
  3792. error = PTR_ERR(from);
  3793. goto exit;
  3794. }
  3795. to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
  3796. if (IS_ERR(to)) {
  3797. error = PTR_ERR(to);
  3798. goto exit1;
  3799. }
  3800. error = -EXDEV;
  3801. if (oldnd.path.mnt != newnd.path.mnt)
  3802. goto exit2;
  3803. old_dir = oldnd.path.dentry;
  3804. error = -EBUSY;
  3805. if (oldnd.last_type != LAST_NORM)
  3806. goto exit2;
  3807. new_dir = newnd.path.dentry;
  3808. if (flags & RENAME_NOREPLACE)
  3809. error = -EEXIST;
  3810. if (newnd.last_type != LAST_NORM)
  3811. goto exit2;
  3812. error = mnt_want_write(oldnd.path.mnt);
  3813. if (error)
  3814. goto exit2;
  3815. oldnd.flags &= ~LOOKUP_PARENT;
  3816. newnd.flags &= ~LOOKUP_PARENT;
  3817. if (!(flags & RENAME_EXCHANGE))
  3818. newnd.flags |= LOOKUP_RENAME_TARGET;
  3819. retry_deleg:
  3820. trap = lock_rename(new_dir, old_dir);
  3821. old_dentry = lookup_hash(&oldnd);
  3822. error = PTR_ERR(old_dentry);
  3823. if (IS_ERR(old_dentry))
  3824. goto exit3;
  3825. /* source must exist */
  3826. error = -ENOENT;
  3827. if (d_is_negative(old_dentry))
  3828. goto exit4;
  3829. new_dentry = lookup_hash(&newnd);
  3830. error = PTR_ERR(new_dentry);
  3831. if (IS_ERR(new_dentry))
  3832. goto exit4;
  3833. error = -EEXIST;
  3834. if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
  3835. goto exit5;
  3836. if (flags & RENAME_EXCHANGE) {
  3837. error = -ENOENT;
  3838. if (d_is_negative(new_dentry))
  3839. goto exit5;
  3840. if (!d_is_dir(new_dentry)) {
  3841. error = -ENOTDIR;
  3842. if (newnd.last.name[newnd.last.len])
  3843. goto exit5;
  3844. }
  3845. }
  3846. /* unless the source is a directory trailing slashes give -ENOTDIR */
  3847. if (!d_is_dir(old_dentry)) {
  3848. error = -ENOTDIR;
  3849. if (oldnd.last.name[oldnd.last.len])
  3850. goto exit5;
  3851. if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
  3852. goto exit5;
  3853. }
  3854. /* source should not be ancestor of target */
  3855. error = -EINVAL;
  3856. if (old_dentry == trap)
  3857. goto exit5;
  3858. /* target should not be an ancestor of source */
  3859. if (!(flags & RENAME_EXCHANGE))
  3860. error = -ENOTEMPTY;
  3861. if (new_dentry == trap)
  3862. goto exit5;
  3863. error = security_path_rename(&oldnd.path, old_dentry,
  3864. &newnd.path, new_dentry, flags);
  3865. if (error)
  3866. goto exit5;
  3867. error = vfs_rename(old_dir->d_inode, old_dentry,
  3868. new_dir->d_inode, new_dentry,
  3869. &delegated_inode, flags);
  3870. exit5:
  3871. dput(new_dentry);
  3872. exit4:
  3873. dput(old_dentry);
  3874. exit3:
  3875. unlock_rename(new_dir, old_dir);
  3876. if (delegated_inode) {
  3877. error = break_deleg_wait(&delegated_inode);
  3878. if (!error)
  3879. goto retry_deleg;
  3880. }
  3881. mnt_drop_write(oldnd.path.mnt);
  3882. exit2:
  3883. if (retry_estale(error, lookup_flags))
  3884. should_retry = true;
  3885. path_put(&newnd.path);
  3886. putname(to);
  3887. exit1:
  3888. path_put(&oldnd.path);
  3889. putname(from);
  3890. if (should_retry) {
  3891. should_retry = false;
  3892. lookup_flags |= LOOKUP_REVAL;
  3893. goto retry;
  3894. }
  3895. exit:
  3896. return error;
  3897. }
  3898. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  3899. int, newdfd, const char __user *, newname)
  3900. {
  3901. return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
  3902. }
  3903. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  3904. {
  3905. return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3906. }
  3907. int vfs_whiteout(struct inode *dir, struct dentry *dentry)
  3908. {
  3909. int error = may_create(dir, dentry);
  3910. if (error)
  3911. return error;
  3912. if (!dir->i_op->mknod)
  3913. return -EPERM;
  3914. return dir->i_op->mknod(dir, dentry,
  3915. S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
  3916. }
  3917. EXPORT_SYMBOL(vfs_whiteout);
  3918. int readlink_copy(char __user *buffer, int buflen, const char *link)
  3919. {
  3920. int len = PTR_ERR(link);
  3921. if (IS_ERR(link))
  3922. goto out;
  3923. len = strlen(link);
  3924. if (len > (unsigned) buflen)
  3925. len = buflen;
  3926. if (copy_to_user(buffer, link, len))
  3927. len = -EFAULT;
  3928. out:
  3929. return len;
  3930. }
  3931. EXPORT_SYMBOL(readlink_copy);
  3932. /*
  3933. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  3934. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  3935. * using) it for any given inode is up to filesystem.
  3936. */
  3937. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3938. {
  3939. struct nameidata nd;
  3940. void *cookie;
  3941. int res;
  3942. nd.depth = 0;
  3943. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  3944. if (IS_ERR(cookie))
  3945. return PTR_ERR(cookie);
  3946. res = readlink_copy(buffer, buflen, nd_get_link(&nd));
  3947. if (dentry->d_inode->i_op->put_link)
  3948. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  3949. return res;
  3950. }
  3951. EXPORT_SYMBOL(generic_readlink);
  3952. /* get the link contents into pagecache */
  3953. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  3954. {
  3955. char *kaddr;
  3956. struct page *page;
  3957. struct address_space *mapping = dentry->d_inode->i_mapping;
  3958. page = read_mapping_page(mapping, 0, NULL);
  3959. if (IS_ERR(page))
  3960. return (char*)page;
  3961. *ppage = page;
  3962. kaddr = kmap(page);
  3963. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  3964. return kaddr;
  3965. }
  3966. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3967. {
  3968. struct page *page = NULL;
  3969. int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
  3970. if (page) {
  3971. kunmap(page);
  3972. page_cache_release(page);
  3973. }
  3974. return res;
  3975. }
  3976. EXPORT_SYMBOL(page_readlink);
  3977. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  3978. {
  3979. struct page *page = NULL;
  3980. nd_set_link(nd, page_getlink(dentry, &page));
  3981. return page;
  3982. }
  3983. EXPORT_SYMBOL(page_follow_link_light);
  3984. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  3985. {
  3986. struct page *page = cookie;
  3987. if (page) {
  3988. kunmap(page);
  3989. page_cache_release(page);
  3990. }
  3991. }
  3992. EXPORT_SYMBOL(page_put_link);
  3993. /*
  3994. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  3995. */
  3996. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  3997. {
  3998. struct address_space *mapping = inode->i_mapping;
  3999. struct page *page;
  4000. void *fsdata;
  4001. int err;
  4002. char *kaddr;
  4003. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  4004. if (nofs)
  4005. flags |= AOP_FLAG_NOFS;
  4006. retry:
  4007. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  4008. flags, &page, &fsdata);
  4009. if (err)
  4010. goto fail;
  4011. kaddr = kmap_atomic(page);
  4012. memcpy(kaddr, symname, len-1);
  4013. kunmap_atomic(kaddr);
  4014. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  4015. page, fsdata);
  4016. if (err < 0)
  4017. goto fail;
  4018. if (err < len-1)
  4019. goto retry;
  4020. mark_inode_dirty(inode);
  4021. return 0;
  4022. fail:
  4023. return err;
  4024. }
  4025. EXPORT_SYMBOL(__page_symlink);
  4026. int page_symlink(struct inode *inode, const char *symname, int len)
  4027. {
  4028. return __page_symlink(inode, symname, len,
  4029. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  4030. }
  4031. EXPORT_SYMBOL(page_symlink);
  4032. const struct inode_operations page_symlink_inode_operations = {
  4033. .readlink = generic_readlink,
  4034. .follow_link = page_follow_link_light,
  4035. .put_link = page_put_link,
  4036. };
  4037. EXPORT_SYMBOL(page_symlink_inode_operations);