namei.c 87 KB

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