namei.c 89 KB

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