namei.c 97 KB

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