namei.c 88 KB

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