namei.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627
  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. /* The new mount record should have at least 2 refs to prevent it being
  824. * expired before we get a chance to add it
  825. */
  826. BUG_ON(mnt_get_count(mnt) < 2);
  827. if (mnt->mnt_sb == path->mnt->mnt_sb &&
  828. mnt->mnt_root == path->dentry) {
  829. mnt_clear_expiry(mnt);
  830. mntput(mnt);
  831. mntput(mnt);
  832. return -ELOOP;
  833. }
  834. /* We need to add the mountpoint to the parent. The filesystem may
  835. * have placed it on an expiry list, and so we need to make sure it
  836. * won't be expired under us if do_add_mount() fails (do_add_mount()
  837. * will eat a reference unconditionally).
  838. */
  839. mntget(mnt);
  840. err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  841. switch (err) {
  842. case -EBUSY:
  843. /* Someone else made a mount here whilst we were busy */
  844. err = 0;
  845. default:
  846. mnt_clear_expiry(mnt);
  847. mntput(mnt);
  848. mntput(mnt);
  849. return err;
  850. case 0:
  851. mntput(mnt);
  852. dput(path->dentry);
  853. if (*need_mntput)
  854. mntput(path->mnt);
  855. path->mnt = mnt;
  856. path->dentry = dget(mnt->mnt_root);
  857. *need_mntput = true;
  858. return 0;
  859. }
  860. }
  861. /*
  862. * Handle a dentry that is managed in some way.
  863. * - Flagged for transit management (autofs)
  864. * - Flagged as mountpoint
  865. * - Flagged as automount point
  866. *
  867. * This may only be called in refwalk mode.
  868. *
  869. * Serialization is taken care of in namespace.c
  870. */
  871. static int follow_managed(struct path *path, unsigned flags)
  872. {
  873. unsigned managed;
  874. bool need_mntput = false;
  875. int ret;
  876. /* Given that we're not holding a lock here, we retain the value in a
  877. * local variable for each dentry as we look at it so that we don't see
  878. * the components of that value change under us */
  879. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  880. managed &= DCACHE_MANAGED_DENTRY,
  881. unlikely(managed != 0)) {
  882. /* Allow the filesystem to manage the transit without i_mutex
  883. * being held. */
  884. if (managed & DCACHE_MANAGE_TRANSIT) {
  885. BUG_ON(!path->dentry->d_op);
  886. BUG_ON(!path->dentry->d_op->d_manage);
  887. ret = path->dentry->d_op->d_manage(path->dentry,
  888. false, false);
  889. if (ret < 0)
  890. return ret == -EISDIR ? 0 : ret;
  891. }
  892. /* Transit to a mounted filesystem. */
  893. if (managed & DCACHE_MOUNTED) {
  894. struct vfsmount *mounted = lookup_mnt(path);
  895. if (mounted) {
  896. dput(path->dentry);
  897. if (need_mntput)
  898. mntput(path->mnt);
  899. path->mnt = mounted;
  900. path->dentry = dget(mounted->mnt_root);
  901. need_mntput = true;
  902. continue;
  903. }
  904. /* Something is mounted on this dentry in another
  905. * namespace and/or whatever was mounted there in this
  906. * namespace got unmounted before we managed to get the
  907. * vfsmount_lock */
  908. }
  909. /* Handle an automount point */
  910. if (managed & DCACHE_NEED_AUTOMOUNT) {
  911. ret = follow_automount(path, flags, &need_mntput);
  912. if (ret < 0)
  913. return ret == -EISDIR ? 0 : ret;
  914. continue;
  915. }
  916. /* We didn't change the current path point */
  917. break;
  918. }
  919. return 0;
  920. }
  921. int follow_down_one(struct path *path)
  922. {
  923. struct vfsmount *mounted;
  924. mounted = lookup_mnt(path);
  925. if (mounted) {
  926. dput(path->dentry);
  927. mntput(path->mnt);
  928. path->mnt = mounted;
  929. path->dentry = dget(mounted->mnt_root);
  930. return 1;
  931. }
  932. return 0;
  933. }
  934. /*
  935. * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
  936. * meet a managed dentry and we're not walking to "..". True is returned to
  937. * continue, false to abort.
  938. */
  939. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  940. struct inode **inode, bool reverse_transit)
  941. {
  942. while (d_mountpoint(path->dentry)) {
  943. struct vfsmount *mounted;
  944. if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
  945. !reverse_transit &&
  946. path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
  947. return false;
  948. mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  949. if (!mounted)
  950. break;
  951. path->mnt = mounted;
  952. path->dentry = mounted->mnt_root;
  953. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  954. *inode = path->dentry->d_inode;
  955. }
  956. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  957. return reverse_transit;
  958. return true;
  959. }
  960. static int follow_dotdot_rcu(struct nameidata *nd)
  961. {
  962. struct inode *inode = nd->inode;
  963. set_root_rcu(nd);
  964. while (1) {
  965. if (nd->path.dentry == nd->root.dentry &&
  966. nd->path.mnt == nd->root.mnt) {
  967. break;
  968. }
  969. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  970. struct dentry *old = nd->path.dentry;
  971. struct dentry *parent = old->d_parent;
  972. unsigned seq;
  973. seq = read_seqcount_begin(&parent->d_seq);
  974. if (read_seqcount_retry(&old->d_seq, nd->seq))
  975. return -ECHILD;
  976. inode = parent->d_inode;
  977. nd->path.dentry = parent;
  978. nd->seq = seq;
  979. break;
  980. }
  981. if (!follow_up_rcu(&nd->path))
  982. break;
  983. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  984. inode = nd->path.dentry->d_inode;
  985. }
  986. __follow_mount_rcu(nd, &nd->path, &inode, true);
  987. nd->inode = inode;
  988. return 0;
  989. }
  990. /*
  991. * Follow down to the covering mount currently visible to userspace. At each
  992. * point, the filesystem owning that dentry may be queried as to whether the
  993. * caller is permitted to proceed or not.
  994. *
  995. * Care must be taken as namespace_sem may be held (indicated by mounting_here
  996. * being true).
  997. */
  998. int follow_down(struct path *path, bool mounting_here)
  999. {
  1000. unsigned managed;
  1001. int ret;
  1002. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1003. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  1004. /* Allow the filesystem to manage the transit without i_mutex
  1005. * being held.
  1006. *
  1007. * We indicate to the filesystem if someone is trying to mount
  1008. * something here. This gives autofs the chance to deny anyone
  1009. * other than its daemon the right to mount on its
  1010. * superstructure.
  1011. *
  1012. * The filesystem may sleep at this point.
  1013. */
  1014. if (managed & DCACHE_MANAGE_TRANSIT) {
  1015. BUG_ON(!path->dentry->d_op);
  1016. BUG_ON(!path->dentry->d_op->d_manage);
  1017. ret = path->dentry->d_op->d_manage(
  1018. path->dentry, mounting_here, false);
  1019. if (ret < 0)
  1020. return ret == -EISDIR ? 0 : ret;
  1021. }
  1022. /* Transit to a mounted filesystem. */
  1023. if (managed & DCACHE_MOUNTED) {
  1024. struct vfsmount *mounted = lookup_mnt(path);
  1025. if (!mounted)
  1026. break;
  1027. dput(path->dentry);
  1028. mntput(path->mnt);
  1029. path->mnt = mounted;
  1030. path->dentry = dget(mounted->mnt_root);
  1031. continue;
  1032. }
  1033. /* Don't handle automount points here */
  1034. break;
  1035. }
  1036. return 0;
  1037. }
  1038. /*
  1039. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1040. */
  1041. static void follow_mount(struct path *path)
  1042. {
  1043. while (d_mountpoint(path->dentry)) {
  1044. struct vfsmount *mounted = lookup_mnt(path);
  1045. if (!mounted)
  1046. break;
  1047. dput(path->dentry);
  1048. mntput(path->mnt);
  1049. path->mnt = mounted;
  1050. path->dentry = dget(mounted->mnt_root);
  1051. }
  1052. }
  1053. static void follow_dotdot(struct nameidata *nd)
  1054. {
  1055. set_root(nd);
  1056. while(1) {
  1057. struct dentry *old = nd->path.dentry;
  1058. if (nd->path.dentry == nd->root.dentry &&
  1059. nd->path.mnt == nd->root.mnt) {
  1060. break;
  1061. }
  1062. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1063. /* rare case of legitimate dget_parent()... */
  1064. nd->path.dentry = dget_parent(nd->path.dentry);
  1065. dput(old);
  1066. break;
  1067. }
  1068. if (!follow_up(&nd->path))
  1069. break;
  1070. }
  1071. follow_mount(&nd->path);
  1072. nd->inode = nd->path.dentry->d_inode;
  1073. }
  1074. /*
  1075. * Allocate a dentry with name and parent, and perform a parent
  1076. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  1077. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  1078. * have verified that no child exists while under i_mutex.
  1079. */
  1080. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  1081. struct qstr *name, struct nameidata *nd)
  1082. {
  1083. struct inode *inode = parent->d_inode;
  1084. struct dentry *dentry;
  1085. struct dentry *old;
  1086. /* Don't create child dentry for a dead directory. */
  1087. if (unlikely(IS_DEADDIR(inode)))
  1088. return ERR_PTR(-ENOENT);
  1089. dentry = d_alloc(parent, name);
  1090. if (unlikely(!dentry))
  1091. return ERR_PTR(-ENOMEM);
  1092. old = inode->i_op->lookup(inode, dentry, nd);
  1093. if (unlikely(old)) {
  1094. dput(dentry);
  1095. dentry = old;
  1096. }
  1097. return dentry;
  1098. }
  1099. /*
  1100. * It's more convoluted than I'd like it to be, but... it's still fairly
  1101. * small and for now I'd prefer to have fast path as straight as possible.
  1102. * It _is_ time-critical.
  1103. */
  1104. static int do_lookup(struct nameidata *nd, struct qstr *name,
  1105. struct path *path, struct inode **inode)
  1106. {
  1107. struct vfsmount *mnt = nd->path.mnt;
  1108. struct dentry *dentry, *parent = nd->path.dentry;
  1109. struct inode *dir;
  1110. int err;
  1111. /*
  1112. * See if the low-level filesystem might want
  1113. * to use its own hash..
  1114. */
  1115. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1116. err = parent->d_op->d_hash(parent, nd->inode, name);
  1117. if (err < 0)
  1118. return err;
  1119. }
  1120. /*
  1121. * Rename seqlock is not required here because in the off chance
  1122. * of a false negative due to a concurrent rename, we're going to
  1123. * do the non-racy lookup, below.
  1124. */
  1125. if (nd->flags & LOOKUP_RCU) {
  1126. unsigned seq;
  1127. *inode = nd->inode;
  1128. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1129. if (!dentry) {
  1130. if (nameidata_drop_rcu(nd))
  1131. return -ECHILD;
  1132. goto need_lookup;
  1133. }
  1134. /* Memory barrier in read_seqcount_begin of child is enough */
  1135. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1136. return -ECHILD;
  1137. nd->seq = seq;
  1138. if (dentry->d_flags & DCACHE_OP_REVALIDATE)
  1139. goto need_revalidate;
  1140. done2:
  1141. path->mnt = mnt;
  1142. path->dentry = dentry;
  1143. if (likely(__follow_mount_rcu(nd, path, inode, false)))
  1144. return 0;
  1145. if (nameidata_drop_rcu(nd))
  1146. return -ECHILD;
  1147. /* fallthru */
  1148. }
  1149. dentry = __d_lookup(parent, name);
  1150. if (!dentry)
  1151. goto need_lookup;
  1152. found:
  1153. if (dentry->d_flags & DCACHE_OP_REVALIDATE)
  1154. goto need_revalidate;
  1155. done:
  1156. path->mnt = mnt;
  1157. path->dentry = dentry;
  1158. err = follow_managed(path, nd->flags);
  1159. if (unlikely(err < 0))
  1160. return err;
  1161. *inode = path->dentry->d_inode;
  1162. return 0;
  1163. need_lookup:
  1164. dir = parent->d_inode;
  1165. BUG_ON(nd->inode != dir);
  1166. mutex_lock(&dir->i_mutex);
  1167. /*
  1168. * First re-do the cached lookup just in case it was created
  1169. * while we waited for the directory semaphore, or the first
  1170. * lookup failed due to an unrelated rename.
  1171. *
  1172. * This could use version numbering or similar to avoid unnecessary
  1173. * cache lookups, but then we'd have to do the first lookup in the
  1174. * non-racy way. However in the common case here, everything should
  1175. * be hot in cache, so would it be a big win?
  1176. */
  1177. dentry = d_lookup(parent, name);
  1178. if (likely(!dentry)) {
  1179. dentry = d_alloc_and_lookup(parent, name, nd);
  1180. mutex_unlock(&dir->i_mutex);
  1181. if (IS_ERR(dentry))
  1182. goto fail;
  1183. goto done;
  1184. }
  1185. /*
  1186. * Uhhuh! Nasty case: the cache was re-populated while
  1187. * we waited on the semaphore. Need to revalidate.
  1188. */
  1189. mutex_unlock(&dir->i_mutex);
  1190. goto found;
  1191. need_revalidate:
  1192. dentry = do_revalidate(dentry, nd);
  1193. if (!dentry)
  1194. goto need_lookup;
  1195. if (IS_ERR(dentry))
  1196. goto fail;
  1197. if (nd->flags & LOOKUP_RCU)
  1198. goto done2;
  1199. goto done;
  1200. fail:
  1201. return PTR_ERR(dentry);
  1202. }
  1203. /*
  1204. * Name resolution.
  1205. * This is the basic name resolution function, turning a pathname into
  1206. * the final dentry. We expect 'base' to be positive and a directory.
  1207. *
  1208. * Returns 0 and nd will have valid dentry and mnt on success.
  1209. * Returns error and drops reference to input namei data on failure.
  1210. */
  1211. static int link_path_walk(const char *name, struct nameidata *nd)
  1212. {
  1213. struct path next;
  1214. int err;
  1215. unsigned int lookup_flags = nd->flags;
  1216. while (*name=='/')
  1217. name++;
  1218. if (!*name)
  1219. goto return_reval;
  1220. if (nd->depth)
  1221. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  1222. /* At this point we know we have a real path component. */
  1223. for(;;) {
  1224. struct inode *inode;
  1225. unsigned long hash;
  1226. struct qstr this;
  1227. unsigned int c;
  1228. nd->flags |= LOOKUP_CONTINUE;
  1229. if (nd->flags & LOOKUP_RCU) {
  1230. err = exec_permission(nd->inode, IPERM_FLAG_RCU);
  1231. if (err == -ECHILD) {
  1232. if (nameidata_drop_rcu(nd))
  1233. return -ECHILD;
  1234. goto exec_again;
  1235. }
  1236. } else {
  1237. exec_again:
  1238. err = exec_permission(nd->inode, 0);
  1239. }
  1240. if (err)
  1241. break;
  1242. this.name = name;
  1243. c = *(const unsigned char *)name;
  1244. hash = init_name_hash();
  1245. do {
  1246. name++;
  1247. hash = partial_name_hash(c, hash);
  1248. c = *(const unsigned char *)name;
  1249. } while (c && (c != '/'));
  1250. this.len = name - (const char *) this.name;
  1251. this.hash = end_name_hash(hash);
  1252. /* remove trailing slashes? */
  1253. if (!c)
  1254. goto last_component;
  1255. while (*++name == '/');
  1256. if (!*name)
  1257. goto last_with_slashes;
  1258. /*
  1259. * "." and ".." are special - ".." especially so because it has
  1260. * to be able to know about the current root directory and
  1261. * parent relationships.
  1262. */
  1263. if (this.name[0] == '.') switch (this.len) {
  1264. default:
  1265. break;
  1266. case 2:
  1267. if (this.name[1] != '.')
  1268. break;
  1269. if (nd->flags & LOOKUP_RCU) {
  1270. if (follow_dotdot_rcu(nd))
  1271. return -ECHILD;
  1272. } else
  1273. follow_dotdot(nd);
  1274. /* fallthrough */
  1275. case 1:
  1276. continue;
  1277. }
  1278. /* This does the actual lookups.. */
  1279. err = do_lookup(nd, &this, &next, &inode);
  1280. if (err)
  1281. break;
  1282. err = -ENOENT;
  1283. if (!inode)
  1284. goto out_dput;
  1285. if (inode->i_op->follow_link) {
  1286. /* We commonly drop rcu-walk here */
  1287. if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
  1288. return -ECHILD;
  1289. BUG_ON(inode != next.dentry->d_inode);
  1290. err = do_follow_link(&next, nd);
  1291. if (err)
  1292. goto return_err;
  1293. nd->inode = nd->path.dentry->d_inode;
  1294. err = -ENOENT;
  1295. if (!nd->inode)
  1296. break;
  1297. } else {
  1298. path_to_nameidata(&next, nd);
  1299. nd->inode = inode;
  1300. }
  1301. err = -ENOTDIR;
  1302. if (!nd->inode->i_op->lookup)
  1303. break;
  1304. continue;
  1305. /* here ends the main loop */
  1306. last_with_slashes:
  1307. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1308. last_component:
  1309. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  1310. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  1311. if (lookup_flags & LOOKUP_PARENT)
  1312. goto lookup_parent;
  1313. if (this.name[0] == '.') switch (this.len) {
  1314. default:
  1315. break;
  1316. case 2:
  1317. if (this.name[1] != '.')
  1318. break;
  1319. if (nd->flags & LOOKUP_RCU) {
  1320. if (follow_dotdot_rcu(nd))
  1321. return -ECHILD;
  1322. } else
  1323. follow_dotdot(nd);
  1324. /* fallthrough */
  1325. case 1:
  1326. goto return_reval;
  1327. }
  1328. err = do_lookup(nd, &this, &next, &inode);
  1329. if (err)
  1330. break;
  1331. if (inode && unlikely(inode->i_op->follow_link) &&
  1332. (lookup_flags & LOOKUP_FOLLOW)) {
  1333. if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
  1334. return -ECHILD;
  1335. BUG_ON(inode != next.dentry->d_inode);
  1336. err = do_follow_link(&next, nd);
  1337. if (err)
  1338. goto return_err;
  1339. nd->inode = nd->path.dentry->d_inode;
  1340. } else {
  1341. path_to_nameidata(&next, nd);
  1342. nd->inode = inode;
  1343. }
  1344. err = -ENOENT;
  1345. if (!nd->inode)
  1346. break;
  1347. if (lookup_flags & LOOKUP_DIRECTORY) {
  1348. err = -ENOTDIR;
  1349. if (!nd->inode->i_op->lookup)
  1350. break;
  1351. }
  1352. goto return_base;
  1353. lookup_parent:
  1354. nd->last = this;
  1355. nd->last_type = LAST_NORM;
  1356. if (this.name[0] != '.')
  1357. goto return_base;
  1358. if (this.len == 1)
  1359. nd->last_type = LAST_DOT;
  1360. else if (this.len == 2 && this.name[1] == '.')
  1361. nd->last_type = LAST_DOTDOT;
  1362. else
  1363. goto return_base;
  1364. return_reval:
  1365. /*
  1366. * We bypassed the ordinary revalidation routines.
  1367. * We may need to check the cached dentry for staleness.
  1368. */
  1369. if (need_reval_dot(nd->path.dentry)) {
  1370. /* Note: we do not d_invalidate() */
  1371. err = d_revalidate(nd->path.dentry, nd);
  1372. if (!err)
  1373. err = -ESTALE;
  1374. if (err < 0)
  1375. break;
  1376. }
  1377. return_base:
  1378. if (nameidata_drop_rcu_last_maybe(nd))
  1379. return -ECHILD;
  1380. return 0;
  1381. out_dput:
  1382. if (!(nd->flags & LOOKUP_RCU))
  1383. path_put_conditional(&next, nd);
  1384. break;
  1385. }
  1386. if (!(nd->flags & LOOKUP_RCU))
  1387. path_put(&nd->path);
  1388. return_err:
  1389. return err;
  1390. }
  1391. static inline int path_walk_rcu(const char *name, struct nameidata *nd)
  1392. {
  1393. current->total_link_count = 0;
  1394. return link_path_walk(name, nd);
  1395. }
  1396. static inline int path_walk_simple(const char *name, struct nameidata *nd)
  1397. {
  1398. current->total_link_count = 0;
  1399. return link_path_walk(name, nd);
  1400. }
  1401. static int path_walk(const char *name, struct nameidata *nd)
  1402. {
  1403. struct path save = nd->path;
  1404. int result;
  1405. current->total_link_count = 0;
  1406. /* make sure the stuff we saved doesn't go away */
  1407. path_get(&save);
  1408. result = link_path_walk(name, nd);
  1409. if (result == -ESTALE) {
  1410. /* nd->path had been dropped */
  1411. current->total_link_count = 0;
  1412. nd->path = save;
  1413. path_get(&nd->path);
  1414. nd->flags |= LOOKUP_REVAL;
  1415. result = link_path_walk(name, nd);
  1416. }
  1417. path_put(&save);
  1418. return result;
  1419. }
  1420. static void path_finish_rcu(struct nameidata *nd)
  1421. {
  1422. if (nd->flags & LOOKUP_RCU) {
  1423. /* RCU dangling. Cancel it. */
  1424. nd->flags &= ~LOOKUP_RCU;
  1425. nd->root.mnt = NULL;
  1426. rcu_read_unlock();
  1427. br_read_unlock(vfsmount_lock);
  1428. }
  1429. if (nd->file)
  1430. fput(nd->file);
  1431. }
  1432. static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
  1433. {
  1434. int retval = 0;
  1435. int fput_needed;
  1436. struct file *file;
  1437. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1438. nd->flags = flags | LOOKUP_RCU;
  1439. nd->depth = 0;
  1440. nd->root.mnt = NULL;
  1441. nd->file = NULL;
  1442. if (*name=='/') {
  1443. struct fs_struct *fs = current->fs;
  1444. unsigned seq;
  1445. br_read_lock(vfsmount_lock);
  1446. rcu_read_lock();
  1447. do {
  1448. seq = read_seqcount_begin(&fs->seq);
  1449. nd->root = fs->root;
  1450. nd->path = nd->root;
  1451. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1452. } while (read_seqcount_retry(&fs->seq, seq));
  1453. } else if (dfd == AT_FDCWD) {
  1454. struct fs_struct *fs = current->fs;
  1455. unsigned seq;
  1456. br_read_lock(vfsmount_lock);
  1457. rcu_read_lock();
  1458. do {
  1459. seq = read_seqcount_begin(&fs->seq);
  1460. nd->path = fs->pwd;
  1461. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1462. } while (read_seqcount_retry(&fs->seq, seq));
  1463. } else {
  1464. struct dentry *dentry;
  1465. file = fget_light(dfd, &fput_needed);
  1466. retval = -EBADF;
  1467. if (!file)
  1468. goto out_fail;
  1469. dentry = file->f_path.dentry;
  1470. retval = -ENOTDIR;
  1471. if (!S_ISDIR(dentry->d_inode->i_mode))
  1472. goto fput_fail;
  1473. retval = file_permission(file, MAY_EXEC);
  1474. if (retval)
  1475. goto fput_fail;
  1476. nd->path = file->f_path;
  1477. if (fput_needed)
  1478. nd->file = file;
  1479. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1480. br_read_lock(vfsmount_lock);
  1481. rcu_read_lock();
  1482. }
  1483. nd->inode = nd->path.dentry->d_inode;
  1484. return 0;
  1485. fput_fail:
  1486. fput_light(file, fput_needed);
  1487. out_fail:
  1488. return retval;
  1489. }
  1490. static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
  1491. {
  1492. int retval = 0;
  1493. int fput_needed;
  1494. struct file *file;
  1495. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1496. nd->flags = flags;
  1497. nd->depth = 0;
  1498. nd->root.mnt = NULL;
  1499. if (*name=='/') {
  1500. set_root(nd);
  1501. nd->path = nd->root;
  1502. path_get(&nd->root);
  1503. } else if (dfd == AT_FDCWD) {
  1504. get_fs_pwd(current->fs, &nd->path);
  1505. } else {
  1506. struct dentry *dentry;
  1507. file = fget_light(dfd, &fput_needed);
  1508. retval = -EBADF;
  1509. if (!file)
  1510. goto out_fail;
  1511. dentry = file->f_path.dentry;
  1512. retval = -ENOTDIR;
  1513. if (!S_ISDIR(dentry->d_inode->i_mode))
  1514. goto fput_fail;
  1515. retval = file_permission(file, MAY_EXEC);
  1516. if (retval)
  1517. goto fput_fail;
  1518. nd->path = file->f_path;
  1519. path_get(&file->f_path);
  1520. fput_light(file, fput_needed);
  1521. }
  1522. nd->inode = nd->path.dentry->d_inode;
  1523. return 0;
  1524. fput_fail:
  1525. fput_light(file, fput_needed);
  1526. out_fail:
  1527. return retval;
  1528. }
  1529. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1530. static int do_path_lookup(int dfd, const char *name,
  1531. unsigned int flags, struct nameidata *nd)
  1532. {
  1533. int retval;
  1534. /*
  1535. * Path walking is largely split up into 2 different synchronisation
  1536. * schemes, rcu-walk and ref-walk (explained in
  1537. * Documentation/filesystems/path-lookup.txt). These share much of the
  1538. * path walk code, but some things particularly setup, cleanup, and
  1539. * following mounts are sufficiently divergent that functions are
  1540. * duplicated. Typically there is a function foo(), and its RCU
  1541. * analogue, foo_rcu().
  1542. *
  1543. * -ECHILD is the error number of choice (just to avoid clashes) that
  1544. * is returned if some aspect of an rcu-walk fails. Such an error must
  1545. * be handled by restarting a traditional ref-walk (which will always
  1546. * be able to complete).
  1547. */
  1548. retval = path_init_rcu(dfd, name, flags, nd);
  1549. if (unlikely(retval))
  1550. return retval;
  1551. retval = path_walk_rcu(name, nd);
  1552. path_finish_rcu(nd);
  1553. if (nd->root.mnt) {
  1554. path_put(&nd->root);
  1555. nd->root.mnt = NULL;
  1556. }
  1557. if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
  1558. /* slower, locked walk */
  1559. if (retval == -ESTALE)
  1560. flags |= LOOKUP_REVAL;
  1561. retval = path_init(dfd, name, flags, nd);
  1562. if (unlikely(retval))
  1563. return retval;
  1564. retval = path_walk(name, nd);
  1565. if (nd->root.mnt) {
  1566. path_put(&nd->root);
  1567. nd->root.mnt = NULL;
  1568. }
  1569. }
  1570. if (likely(!retval)) {
  1571. if (unlikely(!audit_dummy_context())) {
  1572. if (nd->path.dentry && nd->inode)
  1573. audit_inode(name, nd->path.dentry);
  1574. }
  1575. }
  1576. return retval;
  1577. }
  1578. int path_lookup(const char *name, unsigned int flags,
  1579. struct nameidata *nd)
  1580. {
  1581. return do_path_lookup(AT_FDCWD, name, flags, nd);
  1582. }
  1583. int kern_path(const char *name, unsigned int flags, struct path *path)
  1584. {
  1585. struct nameidata nd;
  1586. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1587. if (!res)
  1588. *path = nd.path;
  1589. return res;
  1590. }
  1591. /**
  1592. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1593. * @dentry: pointer to dentry of the base directory
  1594. * @mnt: pointer to vfs mount of the base directory
  1595. * @name: pointer to file name
  1596. * @flags: lookup flags
  1597. * @nd: pointer to nameidata
  1598. */
  1599. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1600. const char *name, unsigned int flags,
  1601. struct nameidata *nd)
  1602. {
  1603. int retval;
  1604. /* same as do_path_lookup */
  1605. nd->last_type = LAST_ROOT;
  1606. nd->flags = flags;
  1607. nd->depth = 0;
  1608. nd->path.dentry = dentry;
  1609. nd->path.mnt = mnt;
  1610. path_get(&nd->path);
  1611. nd->root = nd->path;
  1612. path_get(&nd->root);
  1613. nd->inode = nd->path.dentry->d_inode;
  1614. retval = path_walk(name, nd);
  1615. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  1616. nd->inode))
  1617. audit_inode(name, nd->path.dentry);
  1618. path_put(&nd->root);
  1619. nd->root.mnt = NULL;
  1620. return retval;
  1621. }
  1622. static struct dentry *__lookup_hash(struct qstr *name,
  1623. struct dentry *base, struct nameidata *nd)
  1624. {
  1625. struct inode *inode = base->d_inode;
  1626. struct dentry *dentry;
  1627. int err;
  1628. err = exec_permission(inode, 0);
  1629. if (err)
  1630. return ERR_PTR(err);
  1631. /*
  1632. * See if the low-level filesystem might want
  1633. * to use its own hash..
  1634. */
  1635. if (base->d_flags & DCACHE_OP_HASH) {
  1636. err = base->d_op->d_hash(base, inode, name);
  1637. dentry = ERR_PTR(err);
  1638. if (err < 0)
  1639. goto out;
  1640. }
  1641. /*
  1642. * Don't bother with __d_lookup: callers are for creat as
  1643. * well as unlink, so a lot of the time it would cost
  1644. * a double lookup.
  1645. */
  1646. dentry = d_lookup(base, name);
  1647. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
  1648. dentry = do_revalidate(dentry, nd);
  1649. if (!dentry)
  1650. dentry = d_alloc_and_lookup(base, name, nd);
  1651. out:
  1652. return dentry;
  1653. }
  1654. /*
  1655. * Restricted form of lookup. Doesn't follow links, single-component only,
  1656. * needs parent already locked. Doesn't follow mounts.
  1657. * SMP-safe.
  1658. */
  1659. static struct dentry *lookup_hash(struct nameidata *nd)
  1660. {
  1661. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1662. }
  1663. static int __lookup_one_len(const char *name, struct qstr *this,
  1664. struct dentry *base, int len)
  1665. {
  1666. unsigned long hash;
  1667. unsigned int c;
  1668. this->name = name;
  1669. this->len = len;
  1670. if (!len)
  1671. return -EACCES;
  1672. hash = init_name_hash();
  1673. while (len--) {
  1674. c = *(const unsigned char *)name++;
  1675. if (c == '/' || c == '\0')
  1676. return -EACCES;
  1677. hash = partial_name_hash(c, hash);
  1678. }
  1679. this->hash = end_name_hash(hash);
  1680. return 0;
  1681. }
  1682. /**
  1683. * lookup_one_len - filesystem helper to lookup single pathname component
  1684. * @name: pathname component to lookup
  1685. * @base: base directory to lookup from
  1686. * @len: maximum length @len should be interpreted to
  1687. *
  1688. * Note that this routine is purely a helper for filesystem usage and should
  1689. * not be called by generic code. Also note that by using this function the
  1690. * nameidata argument is passed to the filesystem methods and a filesystem
  1691. * using this helper needs to be prepared for that.
  1692. */
  1693. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1694. {
  1695. int err;
  1696. struct qstr this;
  1697. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1698. err = __lookup_one_len(name, &this, base, len);
  1699. if (err)
  1700. return ERR_PTR(err);
  1701. return __lookup_hash(&this, base, NULL);
  1702. }
  1703. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1704. struct path *path)
  1705. {
  1706. struct nameidata nd;
  1707. char *tmp = getname(name);
  1708. int err = PTR_ERR(tmp);
  1709. if (!IS_ERR(tmp)) {
  1710. BUG_ON(flags & LOOKUP_PARENT);
  1711. err = do_path_lookup(dfd, tmp, flags, &nd);
  1712. putname(tmp);
  1713. if (!err)
  1714. *path = nd.path;
  1715. }
  1716. return err;
  1717. }
  1718. static int user_path_parent(int dfd, const char __user *path,
  1719. struct nameidata *nd, char **name)
  1720. {
  1721. char *s = getname(path);
  1722. int error;
  1723. if (IS_ERR(s))
  1724. return PTR_ERR(s);
  1725. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1726. if (error)
  1727. putname(s);
  1728. else
  1729. *name = s;
  1730. return error;
  1731. }
  1732. /*
  1733. * It's inline, so penalty for filesystems that don't use sticky bit is
  1734. * minimal.
  1735. */
  1736. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1737. {
  1738. uid_t fsuid = current_fsuid();
  1739. if (!(dir->i_mode & S_ISVTX))
  1740. return 0;
  1741. if (inode->i_uid == fsuid)
  1742. return 0;
  1743. if (dir->i_uid == fsuid)
  1744. return 0;
  1745. return !capable(CAP_FOWNER);
  1746. }
  1747. /*
  1748. * Check whether we can remove a link victim from directory dir, check
  1749. * whether the type of victim is right.
  1750. * 1. We can't do it if dir is read-only (done in permission())
  1751. * 2. We should have write and exec permissions on dir
  1752. * 3. We can't remove anything from append-only dir
  1753. * 4. We can't do anything with immutable dir (done in permission())
  1754. * 5. If the sticky bit on dir is set we should either
  1755. * a. be owner of dir, or
  1756. * b. be owner of victim, or
  1757. * c. have CAP_FOWNER capability
  1758. * 6. If the victim is append-only or immutable we can't do antyhing with
  1759. * links pointing to it.
  1760. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1761. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1762. * 9. We can't remove a root or mountpoint.
  1763. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1764. * nfs_async_unlink().
  1765. */
  1766. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1767. {
  1768. int error;
  1769. if (!victim->d_inode)
  1770. return -ENOENT;
  1771. BUG_ON(victim->d_parent->d_inode != dir);
  1772. audit_inode_child(victim, dir);
  1773. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1774. if (error)
  1775. return error;
  1776. if (IS_APPEND(dir))
  1777. return -EPERM;
  1778. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1779. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1780. return -EPERM;
  1781. if (isdir) {
  1782. if (!S_ISDIR(victim->d_inode->i_mode))
  1783. return -ENOTDIR;
  1784. if (IS_ROOT(victim))
  1785. return -EBUSY;
  1786. } else if (S_ISDIR(victim->d_inode->i_mode))
  1787. return -EISDIR;
  1788. if (IS_DEADDIR(dir))
  1789. return -ENOENT;
  1790. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1791. return -EBUSY;
  1792. return 0;
  1793. }
  1794. /* Check whether we can create an object with dentry child in directory
  1795. * dir.
  1796. * 1. We can't do it if child already exists (open has special treatment for
  1797. * this case, but since we are inlined it's OK)
  1798. * 2. We can't do it if dir is read-only (done in permission())
  1799. * 3. We should have write and exec permissions on dir
  1800. * 4. We can't do it if dir is immutable (done in permission())
  1801. */
  1802. static inline int may_create(struct inode *dir, struct dentry *child)
  1803. {
  1804. if (child->d_inode)
  1805. return -EEXIST;
  1806. if (IS_DEADDIR(dir))
  1807. return -ENOENT;
  1808. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1809. }
  1810. /*
  1811. * p1 and p2 should be directories on the same fs.
  1812. */
  1813. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1814. {
  1815. struct dentry *p;
  1816. if (p1 == p2) {
  1817. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1818. return NULL;
  1819. }
  1820. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1821. p = d_ancestor(p2, p1);
  1822. if (p) {
  1823. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1824. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1825. return p;
  1826. }
  1827. p = d_ancestor(p1, p2);
  1828. if (p) {
  1829. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1830. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1831. return p;
  1832. }
  1833. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1834. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1835. return NULL;
  1836. }
  1837. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1838. {
  1839. mutex_unlock(&p1->d_inode->i_mutex);
  1840. if (p1 != p2) {
  1841. mutex_unlock(&p2->d_inode->i_mutex);
  1842. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1843. }
  1844. }
  1845. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1846. struct nameidata *nd)
  1847. {
  1848. int error = may_create(dir, dentry);
  1849. if (error)
  1850. return error;
  1851. if (!dir->i_op->create)
  1852. return -EACCES; /* shouldn't it be ENOSYS? */
  1853. mode &= S_IALLUGO;
  1854. mode |= S_IFREG;
  1855. error = security_inode_create(dir, dentry, mode);
  1856. if (error)
  1857. return error;
  1858. error = dir->i_op->create(dir, dentry, mode, nd);
  1859. if (!error)
  1860. fsnotify_create(dir, dentry);
  1861. return error;
  1862. }
  1863. int may_open(struct path *path, int acc_mode, int flag)
  1864. {
  1865. struct dentry *dentry = path->dentry;
  1866. struct inode *inode = dentry->d_inode;
  1867. int error;
  1868. if (!inode)
  1869. return -ENOENT;
  1870. switch (inode->i_mode & S_IFMT) {
  1871. case S_IFLNK:
  1872. return -ELOOP;
  1873. case S_IFDIR:
  1874. if (acc_mode & MAY_WRITE)
  1875. return -EISDIR;
  1876. break;
  1877. case S_IFBLK:
  1878. case S_IFCHR:
  1879. if (path->mnt->mnt_flags & MNT_NODEV)
  1880. return -EACCES;
  1881. /*FALLTHRU*/
  1882. case S_IFIFO:
  1883. case S_IFSOCK:
  1884. flag &= ~O_TRUNC;
  1885. break;
  1886. }
  1887. error = inode_permission(inode, acc_mode);
  1888. if (error)
  1889. return error;
  1890. /*
  1891. * An append-only file must be opened in append mode for writing.
  1892. */
  1893. if (IS_APPEND(inode)) {
  1894. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1895. return -EPERM;
  1896. if (flag & O_TRUNC)
  1897. return -EPERM;
  1898. }
  1899. /* O_NOATIME can only be set by the owner or superuser */
  1900. if (flag & O_NOATIME && !is_owner_or_cap(inode))
  1901. return -EPERM;
  1902. /*
  1903. * Ensure there are no outstanding leases on the file.
  1904. */
  1905. return break_lease(inode, flag);
  1906. }
  1907. static int handle_truncate(struct file *filp)
  1908. {
  1909. struct path *path = &filp->f_path;
  1910. struct inode *inode = path->dentry->d_inode;
  1911. int error = get_write_access(inode);
  1912. if (error)
  1913. return error;
  1914. /*
  1915. * Refuse to truncate files with mandatory locks held on them.
  1916. */
  1917. error = locks_verify_locked(inode);
  1918. if (!error)
  1919. error = security_path_truncate(path);
  1920. if (!error) {
  1921. error = do_truncate(path->dentry, 0,
  1922. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1923. filp);
  1924. }
  1925. put_write_access(inode);
  1926. return error;
  1927. }
  1928. /*
  1929. * Be careful about ever adding any more callers of this
  1930. * function. Its flags must be in the namei format, not
  1931. * what get passed to sys_open().
  1932. */
  1933. static int __open_namei_create(struct nameidata *nd, struct path *path,
  1934. int open_flag, int mode)
  1935. {
  1936. int error;
  1937. struct dentry *dir = nd->path.dentry;
  1938. if (!IS_POSIXACL(dir->d_inode))
  1939. mode &= ~current_umask();
  1940. error = security_path_mknod(&nd->path, path->dentry, mode, 0);
  1941. if (error)
  1942. goto out_unlock;
  1943. error = vfs_create(dir->d_inode, path->dentry, mode, nd);
  1944. out_unlock:
  1945. mutex_unlock(&dir->d_inode->i_mutex);
  1946. dput(nd->path.dentry);
  1947. nd->path.dentry = path->dentry;
  1948. if (error)
  1949. return error;
  1950. /* Don't check for write permission, don't truncate */
  1951. return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
  1952. }
  1953. /*
  1954. * Note that while the flag value (low two bits) for sys_open means:
  1955. * 00 - read-only
  1956. * 01 - write-only
  1957. * 10 - read-write
  1958. * 11 - special
  1959. * it is changed into
  1960. * 00 - no permissions needed
  1961. * 01 - read-permission
  1962. * 10 - write-permission
  1963. * 11 - read-write
  1964. * for the internal routines (ie open_namei()/follow_link() etc)
  1965. * This is more logical, and also allows the 00 "no perm needed"
  1966. * to be used for symlinks (where the permissions are checked
  1967. * later).
  1968. *
  1969. */
  1970. static inline int open_to_namei_flags(int flag)
  1971. {
  1972. if ((flag+1) & O_ACCMODE)
  1973. flag++;
  1974. return flag;
  1975. }
  1976. static int open_will_truncate(int flag, struct inode *inode)
  1977. {
  1978. /*
  1979. * We'll never write to the fs underlying
  1980. * a device file.
  1981. */
  1982. if (special_file(inode->i_mode))
  1983. return 0;
  1984. return (flag & O_TRUNC);
  1985. }
  1986. static struct file *finish_open(struct nameidata *nd,
  1987. int open_flag, int acc_mode)
  1988. {
  1989. struct file *filp;
  1990. int will_truncate;
  1991. int error;
  1992. will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
  1993. if (will_truncate) {
  1994. error = mnt_want_write(nd->path.mnt);
  1995. if (error)
  1996. goto exit;
  1997. }
  1998. error = may_open(&nd->path, acc_mode, open_flag);
  1999. if (error) {
  2000. if (will_truncate)
  2001. mnt_drop_write(nd->path.mnt);
  2002. goto exit;
  2003. }
  2004. filp = nameidata_to_filp(nd);
  2005. if (!IS_ERR(filp)) {
  2006. error = ima_file_check(filp, acc_mode);
  2007. if (error) {
  2008. fput(filp);
  2009. filp = ERR_PTR(error);
  2010. }
  2011. }
  2012. if (!IS_ERR(filp)) {
  2013. if (will_truncate) {
  2014. error = handle_truncate(filp);
  2015. if (error) {
  2016. fput(filp);
  2017. filp = ERR_PTR(error);
  2018. }
  2019. }
  2020. }
  2021. /*
  2022. * It is now safe to drop the mnt write
  2023. * because the filp has had a write taken
  2024. * on its behalf.
  2025. */
  2026. if (will_truncate)
  2027. mnt_drop_write(nd->path.mnt);
  2028. path_put(&nd->path);
  2029. return filp;
  2030. exit:
  2031. if (!IS_ERR(nd->intent.open.file))
  2032. release_open_intent(nd);
  2033. path_put(&nd->path);
  2034. return ERR_PTR(error);
  2035. }
  2036. /*
  2037. * Handle O_CREAT case for do_filp_open
  2038. */
  2039. static struct file *do_last(struct nameidata *nd, struct path *path,
  2040. int open_flag, int acc_mode,
  2041. int mode, const char *pathname)
  2042. {
  2043. struct dentry *dir = nd->path.dentry;
  2044. struct file *filp;
  2045. int error = -EISDIR;
  2046. switch (nd->last_type) {
  2047. case LAST_DOTDOT:
  2048. follow_dotdot(nd);
  2049. dir = nd->path.dentry;
  2050. case LAST_DOT:
  2051. if (need_reval_dot(dir)) {
  2052. int status = d_revalidate(nd->path.dentry, nd);
  2053. if (!status)
  2054. status = -ESTALE;
  2055. if (status < 0) {
  2056. error = status;
  2057. goto exit;
  2058. }
  2059. }
  2060. /* fallthrough */
  2061. case LAST_ROOT:
  2062. goto exit;
  2063. case LAST_BIND:
  2064. audit_inode(pathname, dir);
  2065. goto ok;
  2066. }
  2067. /* trailing slashes? */
  2068. if (nd->last.name[nd->last.len])
  2069. goto exit;
  2070. mutex_lock(&dir->d_inode->i_mutex);
  2071. path->dentry = lookup_hash(nd);
  2072. path->mnt = nd->path.mnt;
  2073. error = PTR_ERR(path->dentry);
  2074. if (IS_ERR(path->dentry)) {
  2075. mutex_unlock(&dir->d_inode->i_mutex);
  2076. goto exit;
  2077. }
  2078. if (IS_ERR(nd->intent.open.file)) {
  2079. error = PTR_ERR(nd->intent.open.file);
  2080. goto exit_mutex_unlock;
  2081. }
  2082. /* Negative dentry, just create the file */
  2083. if (!path->dentry->d_inode) {
  2084. /*
  2085. * This write is needed to ensure that a
  2086. * ro->rw transition does not occur between
  2087. * the time when the file is created and when
  2088. * a permanent write count is taken through
  2089. * the 'struct file' in nameidata_to_filp().
  2090. */
  2091. error = mnt_want_write(nd->path.mnt);
  2092. if (error)
  2093. goto exit_mutex_unlock;
  2094. error = __open_namei_create(nd, path, open_flag, mode);
  2095. if (error) {
  2096. mnt_drop_write(nd->path.mnt);
  2097. goto exit;
  2098. }
  2099. filp = nameidata_to_filp(nd);
  2100. mnt_drop_write(nd->path.mnt);
  2101. path_put(&nd->path);
  2102. if (!IS_ERR(filp)) {
  2103. error = ima_file_check(filp, acc_mode);
  2104. if (error) {
  2105. fput(filp);
  2106. filp = ERR_PTR(error);
  2107. }
  2108. }
  2109. return filp;
  2110. }
  2111. /*
  2112. * It already exists.
  2113. */
  2114. mutex_unlock(&dir->d_inode->i_mutex);
  2115. audit_inode(pathname, path->dentry);
  2116. error = -EEXIST;
  2117. if (open_flag & O_EXCL)
  2118. goto exit_dput;
  2119. error = follow_managed(path, nd->flags);
  2120. if (error < 0)
  2121. goto exit_dput;
  2122. error = -ENOENT;
  2123. if (!path->dentry->d_inode)
  2124. goto exit_dput;
  2125. if (path->dentry->d_inode->i_op->follow_link)
  2126. return NULL;
  2127. path_to_nameidata(path, nd);
  2128. nd->inode = path->dentry->d_inode;
  2129. error = -EISDIR;
  2130. if (S_ISDIR(nd->inode->i_mode))
  2131. goto exit;
  2132. ok:
  2133. filp = finish_open(nd, open_flag, acc_mode);
  2134. return filp;
  2135. exit_mutex_unlock:
  2136. mutex_unlock(&dir->d_inode->i_mutex);
  2137. exit_dput:
  2138. path_put_conditional(path, nd);
  2139. exit:
  2140. if (!IS_ERR(nd->intent.open.file))
  2141. release_open_intent(nd);
  2142. path_put(&nd->path);
  2143. return ERR_PTR(error);
  2144. }
  2145. /*
  2146. * Note that the low bits of the passed in "open_flag"
  2147. * are not the same as in the local variable "flag". See
  2148. * open_to_namei_flags() for more details.
  2149. */
  2150. struct file *do_filp_open(int dfd, const char *pathname,
  2151. int open_flag, int mode, int acc_mode)
  2152. {
  2153. struct file *filp;
  2154. struct nameidata nd;
  2155. int error;
  2156. struct path path;
  2157. int count = 0;
  2158. int flag = open_to_namei_flags(open_flag);
  2159. int flags;
  2160. if (!(open_flag & O_CREAT))
  2161. mode = 0;
  2162. /* Must never be set by userspace */
  2163. open_flag &= ~FMODE_NONOTIFY;
  2164. /*
  2165. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  2166. * check for O_DSYNC if the need any syncing at all we enforce it's
  2167. * always set instead of having to deal with possibly weird behaviour
  2168. * for malicious applications setting only __O_SYNC.
  2169. */
  2170. if (open_flag & __O_SYNC)
  2171. open_flag |= O_DSYNC;
  2172. if (!acc_mode)
  2173. acc_mode = MAY_OPEN | ACC_MODE(open_flag);
  2174. /* O_TRUNC implies we need access checks for write permissions */
  2175. if (open_flag & O_TRUNC)
  2176. acc_mode |= MAY_WRITE;
  2177. /* Allow the LSM permission hook to distinguish append
  2178. access from general write access. */
  2179. if (open_flag & O_APPEND)
  2180. acc_mode |= MAY_APPEND;
  2181. flags = LOOKUP_OPEN;
  2182. if (open_flag & O_CREAT) {
  2183. flags |= LOOKUP_CREATE;
  2184. if (open_flag & O_EXCL)
  2185. flags |= LOOKUP_EXCL;
  2186. }
  2187. if (open_flag & O_DIRECTORY)
  2188. flags |= LOOKUP_DIRECTORY;
  2189. if (!(open_flag & O_NOFOLLOW))
  2190. flags |= LOOKUP_FOLLOW;
  2191. filp = get_empty_filp();
  2192. if (!filp)
  2193. return ERR_PTR(-ENFILE);
  2194. filp->f_flags = open_flag;
  2195. nd.intent.open.file = filp;
  2196. nd.intent.open.flags = flag;
  2197. nd.intent.open.create_mode = mode;
  2198. if (open_flag & O_CREAT)
  2199. goto creat;
  2200. /* !O_CREAT, simple open */
  2201. error = do_path_lookup(dfd, pathname, flags, &nd);
  2202. if (unlikely(error))
  2203. goto out_filp;
  2204. error = -ELOOP;
  2205. if (!(nd.flags & LOOKUP_FOLLOW)) {
  2206. if (nd.inode->i_op->follow_link)
  2207. goto out_path;
  2208. }
  2209. error = -ENOTDIR;
  2210. if (nd.flags & LOOKUP_DIRECTORY) {
  2211. if (!nd.inode->i_op->lookup)
  2212. goto out_path;
  2213. }
  2214. audit_inode(pathname, nd.path.dentry);
  2215. filp = finish_open(&nd, open_flag, acc_mode);
  2216. return filp;
  2217. creat:
  2218. /* OK, have to create the file. Find the parent. */
  2219. error = path_init_rcu(dfd, pathname,
  2220. LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
  2221. if (error)
  2222. goto out_filp;
  2223. error = path_walk_rcu(pathname, &nd);
  2224. path_finish_rcu(&nd);
  2225. if (unlikely(error == -ECHILD || error == -ESTALE)) {
  2226. /* slower, locked walk */
  2227. if (error == -ESTALE) {
  2228. reval:
  2229. flags |= LOOKUP_REVAL;
  2230. }
  2231. error = path_init(dfd, pathname,
  2232. LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
  2233. if (error)
  2234. goto out_filp;
  2235. error = path_walk_simple(pathname, &nd);
  2236. }
  2237. if (unlikely(error))
  2238. goto out_filp;
  2239. if (unlikely(!audit_dummy_context()))
  2240. audit_inode(pathname, nd.path.dentry);
  2241. /*
  2242. * We have the parent and last component.
  2243. */
  2244. nd.flags = flags;
  2245. filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
  2246. while (unlikely(!filp)) { /* trailing symlink */
  2247. struct path link = path;
  2248. struct inode *linki = link.dentry->d_inode;
  2249. void *cookie;
  2250. error = -ELOOP;
  2251. if (!(nd.flags & LOOKUP_FOLLOW))
  2252. goto exit_dput;
  2253. if (count++ == 32)
  2254. goto exit_dput;
  2255. /*
  2256. * This is subtle. Instead of calling do_follow_link() we do
  2257. * the thing by hands. The reason is that this way we have zero
  2258. * link_count and path_walk() (called from ->follow_link)
  2259. * honoring LOOKUP_PARENT. After that we have the parent and
  2260. * last component, i.e. we are in the same situation as after
  2261. * the first path_walk(). Well, almost - if the last component
  2262. * is normal we get its copy stored in nd->last.name and we will
  2263. * have to putname() it when we are done. Procfs-like symlinks
  2264. * just set LAST_BIND.
  2265. */
  2266. nd.flags |= LOOKUP_PARENT;
  2267. error = security_inode_follow_link(link.dentry, &nd);
  2268. if (error)
  2269. goto exit_dput;
  2270. error = __do_follow_link(&link, &nd, &cookie);
  2271. if (unlikely(error)) {
  2272. if (!IS_ERR(cookie) && linki->i_op->put_link)
  2273. linki->i_op->put_link(link.dentry, &nd, cookie);
  2274. /* nd.path had been dropped */
  2275. nd.path = link;
  2276. goto out_path;
  2277. }
  2278. nd.flags &= ~LOOKUP_PARENT;
  2279. filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
  2280. if (linki->i_op->put_link)
  2281. linki->i_op->put_link(link.dentry, &nd, cookie);
  2282. path_put(&link);
  2283. }
  2284. out:
  2285. if (nd.root.mnt)
  2286. path_put(&nd.root);
  2287. if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
  2288. goto reval;
  2289. return filp;
  2290. exit_dput:
  2291. path_put_conditional(&path, &nd);
  2292. out_path:
  2293. path_put(&nd.path);
  2294. out_filp:
  2295. if (!IS_ERR(nd.intent.open.file))
  2296. release_open_intent(&nd);
  2297. filp = ERR_PTR(error);
  2298. goto out;
  2299. }
  2300. /**
  2301. * filp_open - open file and return file pointer
  2302. *
  2303. * @filename: path to open
  2304. * @flags: open flags as per the open(2) second argument
  2305. * @mode: mode for the new file if O_CREAT is set, else ignored
  2306. *
  2307. * This is the helper to open a file from kernelspace if you really
  2308. * have to. But in generally you should not do this, so please move
  2309. * along, nothing to see here..
  2310. */
  2311. struct file *filp_open(const char *filename, int flags, int mode)
  2312. {
  2313. return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
  2314. }
  2315. EXPORT_SYMBOL(filp_open);
  2316. /**
  2317. * lookup_create - lookup a dentry, creating it if it doesn't exist
  2318. * @nd: nameidata info
  2319. * @is_dir: directory flag
  2320. *
  2321. * Simple function to lookup and return a dentry and create it
  2322. * if it doesn't exist. Is SMP-safe.
  2323. *
  2324. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  2325. */
  2326. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  2327. {
  2328. struct dentry *dentry = ERR_PTR(-EEXIST);
  2329. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2330. /*
  2331. * Yucky last component or no last component at all?
  2332. * (foo/., foo/.., /////)
  2333. */
  2334. if (nd->last_type != LAST_NORM)
  2335. goto fail;
  2336. nd->flags &= ~LOOKUP_PARENT;
  2337. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2338. nd->intent.open.flags = O_EXCL;
  2339. /*
  2340. * Do the final lookup.
  2341. */
  2342. dentry = lookup_hash(nd);
  2343. if (IS_ERR(dentry))
  2344. goto fail;
  2345. if (dentry->d_inode)
  2346. goto eexist;
  2347. /*
  2348. * Special case - lookup gave negative, but... we had foo/bar/
  2349. * From the vfs_mknod() POV we just have a negative dentry -
  2350. * all is fine. Let's be bastards - you had / on the end, you've
  2351. * been asking for (non-existent) directory. -ENOENT for you.
  2352. */
  2353. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  2354. dput(dentry);
  2355. dentry = ERR_PTR(-ENOENT);
  2356. }
  2357. return dentry;
  2358. eexist:
  2359. dput(dentry);
  2360. dentry = ERR_PTR(-EEXIST);
  2361. fail:
  2362. return dentry;
  2363. }
  2364. EXPORT_SYMBOL_GPL(lookup_create);
  2365. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2366. {
  2367. int error = may_create(dir, dentry);
  2368. if (error)
  2369. return error;
  2370. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  2371. return -EPERM;
  2372. if (!dir->i_op->mknod)
  2373. return -EPERM;
  2374. error = devcgroup_inode_mknod(mode, dev);
  2375. if (error)
  2376. return error;
  2377. error = security_inode_mknod(dir, dentry, mode, dev);
  2378. if (error)
  2379. return error;
  2380. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2381. if (!error)
  2382. fsnotify_create(dir, dentry);
  2383. return error;
  2384. }
  2385. static int may_mknod(mode_t mode)
  2386. {
  2387. switch (mode & S_IFMT) {
  2388. case S_IFREG:
  2389. case S_IFCHR:
  2390. case S_IFBLK:
  2391. case S_IFIFO:
  2392. case S_IFSOCK:
  2393. case 0: /* zero mode translates to S_IFREG */
  2394. return 0;
  2395. case S_IFDIR:
  2396. return -EPERM;
  2397. default:
  2398. return -EINVAL;
  2399. }
  2400. }
  2401. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2402. unsigned, dev)
  2403. {
  2404. int error;
  2405. char *tmp;
  2406. struct dentry *dentry;
  2407. struct nameidata nd;
  2408. if (S_ISDIR(mode))
  2409. return -EPERM;
  2410. error = user_path_parent(dfd, filename, &nd, &tmp);
  2411. if (error)
  2412. return error;
  2413. dentry = lookup_create(&nd, 0);
  2414. if (IS_ERR(dentry)) {
  2415. error = PTR_ERR(dentry);
  2416. goto out_unlock;
  2417. }
  2418. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2419. mode &= ~current_umask();
  2420. error = may_mknod(mode);
  2421. if (error)
  2422. goto out_dput;
  2423. error = mnt_want_write(nd.path.mnt);
  2424. if (error)
  2425. goto out_dput;
  2426. error = security_path_mknod(&nd.path, dentry, mode, dev);
  2427. if (error)
  2428. goto out_drop_write;
  2429. switch (mode & S_IFMT) {
  2430. case 0: case S_IFREG:
  2431. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  2432. break;
  2433. case S_IFCHR: case S_IFBLK:
  2434. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  2435. new_decode_dev(dev));
  2436. break;
  2437. case S_IFIFO: case S_IFSOCK:
  2438. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  2439. break;
  2440. }
  2441. out_drop_write:
  2442. mnt_drop_write(nd.path.mnt);
  2443. out_dput:
  2444. dput(dentry);
  2445. out_unlock:
  2446. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2447. path_put(&nd.path);
  2448. putname(tmp);
  2449. return error;
  2450. }
  2451. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2452. {
  2453. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2454. }
  2455. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2456. {
  2457. int error = may_create(dir, dentry);
  2458. if (error)
  2459. return error;
  2460. if (!dir->i_op->mkdir)
  2461. return -EPERM;
  2462. mode &= (S_IRWXUGO|S_ISVTX);
  2463. error = security_inode_mkdir(dir, dentry, mode);
  2464. if (error)
  2465. return error;
  2466. error = dir->i_op->mkdir(dir, dentry, mode);
  2467. if (!error)
  2468. fsnotify_mkdir(dir, dentry);
  2469. return error;
  2470. }
  2471. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2472. {
  2473. int error = 0;
  2474. char * tmp;
  2475. struct dentry *dentry;
  2476. struct nameidata nd;
  2477. error = user_path_parent(dfd, pathname, &nd, &tmp);
  2478. if (error)
  2479. goto out_err;
  2480. dentry = lookup_create(&nd, 1);
  2481. error = PTR_ERR(dentry);
  2482. if (IS_ERR(dentry))
  2483. goto out_unlock;
  2484. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2485. mode &= ~current_umask();
  2486. error = mnt_want_write(nd.path.mnt);
  2487. if (error)
  2488. goto out_dput;
  2489. error = security_path_mkdir(&nd.path, dentry, mode);
  2490. if (error)
  2491. goto out_drop_write;
  2492. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  2493. out_drop_write:
  2494. mnt_drop_write(nd.path.mnt);
  2495. out_dput:
  2496. dput(dentry);
  2497. out_unlock:
  2498. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2499. path_put(&nd.path);
  2500. putname(tmp);
  2501. out_err:
  2502. return error;
  2503. }
  2504. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2505. {
  2506. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2507. }
  2508. /*
  2509. * We try to drop the dentry early: we should have
  2510. * a usage count of 2 if we're the only user of this
  2511. * dentry, and if that is true (possibly after pruning
  2512. * the dcache), then we drop the dentry now.
  2513. *
  2514. * A low-level filesystem can, if it choses, legally
  2515. * do a
  2516. *
  2517. * if (!d_unhashed(dentry))
  2518. * return -EBUSY;
  2519. *
  2520. * if it cannot handle the case of removing a directory
  2521. * that is still in use by something else..
  2522. */
  2523. void dentry_unhash(struct dentry *dentry)
  2524. {
  2525. dget(dentry);
  2526. shrink_dcache_parent(dentry);
  2527. spin_lock(&dentry->d_lock);
  2528. if (dentry->d_count == 2)
  2529. __d_drop(dentry);
  2530. spin_unlock(&dentry->d_lock);
  2531. }
  2532. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2533. {
  2534. int error = may_delete(dir, dentry, 1);
  2535. if (error)
  2536. return error;
  2537. if (!dir->i_op->rmdir)
  2538. return -EPERM;
  2539. mutex_lock(&dentry->d_inode->i_mutex);
  2540. dentry_unhash(dentry);
  2541. if (d_mountpoint(dentry))
  2542. error = -EBUSY;
  2543. else {
  2544. error = security_inode_rmdir(dir, dentry);
  2545. if (!error) {
  2546. error = dir->i_op->rmdir(dir, dentry);
  2547. if (!error) {
  2548. dentry->d_inode->i_flags |= S_DEAD;
  2549. dont_mount(dentry);
  2550. }
  2551. }
  2552. }
  2553. mutex_unlock(&dentry->d_inode->i_mutex);
  2554. if (!error) {
  2555. d_delete(dentry);
  2556. }
  2557. dput(dentry);
  2558. return error;
  2559. }
  2560. static long do_rmdir(int dfd, const char __user *pathname)
  2561. {
  2562. int error = 0;
  2563. char * name;
  2564. struct dentry *dentry;
  2565. struct nameidata nd;
  2566. error = user_path_parent(dfd, pathname, &nd, &name);
  2567. if (error)
  2568. return error;
  2569. switch(nd.last_type) {
  2570. case LAST_DOTDOT:
  2571. error = -ENOTEMPTY;
  2572. goto exit1;
  2573. case LAST_DOT:
  2574. error = -EINVAL;
  2575. goto exit1;
  2576. case LAST_ROOT:
  2577. error = -EBUSY;
  2578. goto exit1;
  2579. }
  2580. nd.flags &= ~LOOKUP_PARENT;
  2581. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2582. dentry = lookup_hash(&nd);
  2583. error = PTR_ERR(dentry);
  2584. if (IS_ERR(dentry))
  2585. goto exit2;
  2586. error = mnt_want_write(nd.path.mnt);
  2587. if (error)
  2588. goto exit3;
  2589. error = security_path_rmdir(&nd.path, dentry);
  2590. if (error)
  2591. goto exit4;
  2592. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2593. exit4:
  2594. mnt_drop_write(nd.path.mnt);
  2595. exit3:
  2596. dput(dentry);
  2597. exit2:
  2598. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2599. exit1:
  2600. path_put(&nd.path);
  2601. putname(name);
  2602. return error;
  2603. }
  2604. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2605. {
  2606. return do_rmdir(AT_FDCWD, pathname);
  2607. }
  2608. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2609. {
  2610. int error = may_delete(dir, dentry, 0);
  2611. if (error)
  2612. return error;
  2613. if (!dir->i_op->unlink)
  2614. return -EPERM;
  2615. mutex_lock(&dentry->d_inode->i_mutex);
  2616. if (d_mountpoint(dentry))
  2617. error = -EBUSY;
  2618. else {
  2619. error = security_inode_unlink(dir, dentry);
  2620. if (!error) {
  2621. error = dir->i_op->unlink(dir, dentry);
  2622. if (!error)
  2623. dont_mount(dentry);
  2624. }
  2625. }
  2626. mutex_unlock(&dentry->d_inode->i_mutex);
  2627. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2628. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2629. fsnotify_link_count(dentry->d_inode);
  2630. d_delete(dentry);
  2631. }
  2632. return error;
  2633. }
  2634. /*
  2635. * Make sure that the actual truncation of the file will occur outside its
  2636. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2637. * writeout happening, and we don't want to prevent access to the directory
  2638. * while waiting on the I/O.
  2639. */
  2640. static long do_unlinkat(int dfd, const char __user *pathname)
  2641. {
  2642. int error;
  2643. char *name;
  2644. struct dentry *dentry;
  2645. struct nameidata nd;
  2646. struct inode *inode = NULL;
  2647. error = user_path_parent(dfd, pathname, &nd, &name);
  2648. if (error)
  2649. return error;
  2650. error = -EISDIR;
  2651. if (nd.last_type != LAST_NORM)
  2652. goto exit1;
  2653. nd.flags &= ~LOOKUP_PARENT;
  2654. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2655. dentry = lookup_hash(&nd);
  2656. error = PTR_ERR(dentry);
  2657. if (!IS_ERR(dentry)) {
  2658. /* Why not before? Because we want correct error value */
  2659. if (nd.last.name[nd.last.len])
  2660. goto slashes;
  2661. inode = dentry->d_inode;
  2662. if (inode)
  2663. ihold(inode);
  2664. error = mnt_want_write(nd.path.mnt);
  2665. if (error)
  2666. goto exit2;
  2667. error = security_path_unlink(&nd.path, dentry);
  2668. if (error)
  2669. goto exit3;
  2670. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2671. exit3:
  2672. mnt_drop_write(nd.path.mnt);
  2673. exit2:
  2674. dput(dentry);
  2675. }
  2676. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2677. if (inode)
  2678. iput(inode); /* truncate the inode here */
  2679. exit1:
  2680. path_put(&nd.path);
  2681. putname(name);
  2682. return error;
  2683. slashes:
  2684. error = !dentry->d_inode ? -ENOENT :
  2685. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2686. goto exit2;
  2687. }
  2688. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2689. {
  2690. if ((flag & ~AT_REMOVEDIR) != 0)
  2691. return -EINVAL;
  2692. if (flag & AT_REMOVEDIR)
  2693. return do_rmdir(dfd, pathname);
  2694. return do_unlinkat(dfd, pathname);
  2695. }
  2696. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2697. {
  2698. return do_unlinkat(AT_FDCWD, pathname);
  2699. }
  2700. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2701. {
  2702. int error = may_create(dir, dentry);
  2703. if (error)
  2704. return error;
  2705. if (!dir->i_op->symlink)
  2706. return -EPERM;
  2707. error = security_inode_symlink(dir, dentry, oldname);
  2708. if (error)
  2709. return error;
  2710. error = dir->i_op->symlink(dir, dentry, oldname);
  2711. if (!error)
  2712. fsnotify_create(dir, dentry);
  2713. return error;
  2714. }
  2715. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2716. int, newdfd, const char __user *, newname)
  2717. {
  2718. int error;
  2719. char *from;
  2720. char *to;
  2721. struct dentry *dentry;
  2722. struct nameidata nd;
  2723. from = getname(oldname);
  2724. if (IS_ERR(from))
  2725. return PTR_ERR(from);
  2726. error = user_path_parent(newdfd, newname, &nd, &to);
  2727. if (error)
  2728. goto out_putname;
  2729. dentry = lookup_create(&nd, 0);
  2730. error = PTR_ERR(dentry);
  2731. if (IS_ERR(dentry))
  2732. goto out_unlock;
  2733. error = mnt_want_write(nd.path.mnt);
  2734. if (error)
  2735. goto out_dput;
  2736. error = security_path_symlink(&nd.path, dentry, from);
  2737. if (error)
  2738. goto out_drop_write;
  2739. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2740. out_drop_write:
  2741. mnt_drop_write(nd.path.mnt);
  2742. out_dput:
  2743. dput(dentry);
  2744. out_unlock:
  2745. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2746. path_put(&nd.path);
  2747. putname(to);
  2748. out_putname:
  2749. putname(from);
  2750. return error;
  2751. }
  2752. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2753. {
  2754. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2755. }
  2756. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2757. {
  2758. struct inode *inode = old_dentry->d_inode;
  2759. int error;
  2760. if (!inode)
  2761. return -ENOENT;
  2762. error = may_create(dir, new_dentry);
  2763. if (error)
  2764. return error;
  2765. if (dir->i_sb != inode->i_sb)
  2766. return -EXDEV;
  2767. /*
  2768. * A link to an append-only or immutable file cannot be created.
  2769. */
  2770. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2771. return -EPERM;
  2772. if (!dir->i_op->link)
  2773. return -EPERM;
  2774. if (S_ISDIR(inode->i_mode))
  2775. return -EPERM;
  2776. error = security_inode_link(old_dentry, dir, new_dentry);
  2777. if (error)
  2778. return error;
  2779. mutex_lock(&inode->i_mutex);
  2780. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2781. mutex_unlock(&inode->i_mutex);
  2782. if (!error)
  2783. fsnotify_link(dir, inode, new_dentry);
  2784. return error;
  2785. }
  2786. /*
  2787. * Hardlinks are often used in delicate situations. We avoid
  2788. * security-related surprises by not following symlinks on the
  2789. * newname. --KAB
  2790. *
  2791. * We don't follow them on the oldname either to be compatible
  2792. * with linux 2.0, and to avoid hard-linking to directories
  2793. * and other special files. --ADM
  2794. */
  2795. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2796. int, newdfd, const char __user *, newname, int, flags)
  2797. {
  2798. struct dentry *new_dentry;
  2799. struct nameidata nd;
  2800. struct path old_path;
  2801. int error;
  2802. char *to;
  2803. if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
  2804. return -EINVAL;
  2805. error = user_path_at(olddfd, oldname,
  2806. flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
  2807. &old_path);
  2808. if (error)
  2809. return error;
  2810. error = user_path_parent(newdfd, newname, &nd, &to);
  2811. if (error)
  2812. goto out;
  2813. error = -EXDEV;
  2814. if (old_path.mnt != nd.path.mnt)
  2815. goto out_release;
  2816. new_dentry = lookup_create(&nd, 0);
  2817. error = PTR_ERR(new_dentry);
  2818. if (IS_ERR(new_dentry))
  2819. goto out_unlock;
  2820. error = mnt_want_write(nd.path.mnt);
  2821. if (error)
  2822. goto out_dput;
  2823. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2824. if (error)
  2825. goto out_drop_write;
  2826. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2827. out_drop_write:
  2828. mnt_drop_write(nd.path.mnt);
  2829. out_dput:
  2830. dput(new_dentry);
  2831. out_unlock:
  2832. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2833. out_release:
  2834. path_put(&nd.path);
  2835. putname(to);
  2836. out:
  2837. path_put(&old_path);
  2838. return error;
  2839. }
  2840. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2841. {
  2842. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2843. }
  2844. /*
  2845. * The worst of all namespace operations - renaming directory. "Perverted"
  2846. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2847. * Problems:
  2848. * a) we can get into loop creation. Check is done in is_subdir().
  2849. * b) race potential - two innocent renames can create a loop together.
  2850. * That's where 4.4 screws up. Current fix: serialization on
  2851. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2852. * story.
  2853. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2854. * And that - after we got ->i_mutex on parents (until then we don't know
  2855. * whether the target exists). Solution: try to be smart with locking
  2856. * order for inodes. We rely on the fact that tree topology may change
  2857. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2858. * move will be locked. Thus we can rank directories by the tree
  2859. * (ancestors first) and rank all non-directories after them.
  2860. * That works since everybody except rename does "lock parent, lookup,
  2861. * lock child" and rename is under ->s_vfs_rename_mutex.
  2862. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2863. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2864. * we'd better make sure that there's no link(2) for them.
  2865. * d) some filesystems don't support opened-but-unlinked directories,
  2866. * either because of layout or because they are not ready to deal with
  2867. * all cases correctly. The latter will be fixed (taking this sort of
  2868. * stuff into VFS), but the former is not going away. Solution: the same
  2869. * trick as in rmdir().
  2870. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2871. * we are removing the target. Solution: we will have to grab ->i_mutex
  2872. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2873. * ->i_mutex on parents, which works but leads to some truly excessive
  2874. * locking].
  2875. */
  2876. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2877. struct inode *new_dir, struct dentry *new_dentry)
  2878. {
  2879. int error = 0;
  2880. struct inode *target;
  2881. /*
  2882. * If we are going to change the parent - check write permissions,
  2883. * we'll need to flip '..'.
  2884. */
  2885. if (new_dir != old_dir) {
  2886. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2887. if (error)
  2888. return error;
  2889. }
  2890. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2891. if (error)
  2892. return error;
  2893. target = new_dentry->d_inode;
  2894. if (target)
  2895. mutex_lock(&target->i_mutex);
  2896. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2897. error = -EBUSY;
  2898. else {
  2899. if (target)
  2900. dentry_unhash(new_dentry);
  2901. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2902. }
  2903. if (target) {
  2904. if (!error) {
  2905. target->i_flags |= S_DEAD;
  2906. dont_mount(new_dentry);
  2907. }
  2908. mutex_unlock(&target->i_mutex);
  2909. if (d_unhashed(new_dentry))
  2910. d_rehash(new_dentry);
  2911. dput(new_dentry);
  2912. }
  2913. if (!error)
  2914. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2915. d_move(old_dentry,new_dentry);
  2916. return error;
  2917. }
  2918. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2919. struct inode *new_dir, struct dentry *new_dentry)
  2920. {
  2921. struct inode *target;
  2922. int error;
  2923. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2924. if (error)
  2925. return error;
  2926. dget(new_dentry);
  2927. target = new_dentry->d_inode;
  2928. if (target)
  2929. mutex_lock(&target->i_mutex);
  2930. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2931. error = -EBUSY;
  2932. else
  2933. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2934. if (!error) {
  2935. if (target)
  2936. dont_mount(new_dentry);
  2937. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2938. d_move(old_dentry, new_dentry);
  2939. }
  2940. if (target)
  2941. mutex_unlock(&target->i_mutex);
  2942. dput(new_dentry);
  2943. return error;
  2944. }
  2945. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2946. struct inode *new_dir, struct dentry *new_dentry)
  2947. {
  2948. int error;
  2949. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2950. const unsigned char *old_name;
  2951. if (old_dentry->d_inode == new_dentry->d_inode)
  2952. return 0;
  2953. error = may_delete(old_dir, old_dentry, is_dir);
  2954. if (error)
  2955. return error;
  2956. if (!new_dentry->d_inode)
  2957. error = may_create(new_dir, new_dentry);
  2958. else
  2959. error = may_delete(new_dir, new_dentry, is_dir);
  2960. if (error)
  2961. return error;
  2962. if (!old_dir->i_op->rename)
  2963. return -EPERM;
  2964. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2965. if (is_dir)
  2966. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2967. else
  2968. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2969. if (!error)
  2970. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2971. new_dentry->d_inode, old_dentry);
  2972. fsnotify_oldname_free(old_name);
  2973. return error;
  2974. }
  2975. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2976. int, newdfd, const char __user *, newname)
  2977. {
  2978. struct dentry *old_dir, *new_dir;
  2979. struct dentry *old_dentry, *new_dentry;
  2980. struct dentry *trap;
  2981. struct nameidata oldnd, newnd;
  2982. char *from;
  2983. char *to;
  2984. int error;
  2985. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2986. if (error)
  2987. goto exit;
  2988. error = user_path_parent(newdfd, newname, &newnd, &to);
  2989. if (error)
  2990. goto exit1;
  2991. error = -EXDEV;
  2992. if (oldnd.path.mnt != newnd.path.mnt)
  2993. goto exit2;
  2994. old_dir = oldnd.path.dentry;
  2995. error = -EBUSY;
  2996. if (oldnd.last_type != LAST_NORM)
  2997. goto exit2;
  2998. new_dir = newnd.path.dentry;
  2999. if (newnd.last_type != LAST_NORM)
  3000. goto exit2;
  3001. oldnd.flags &= ~LOOKUP_PARENT;
  3002. newnd.flags &= ~LOOKUP_PARENT;
  3003. newnd.flags |= LOOKUP_RENAME_TARGET;
  3004. trap = lock_rename(new_dir, old_dir);
  3005. old_dentry = lookup_hash(&oldnd);
  3006. error = PTR_ERR(old_dentry);
  3007. if (IS_ERR(old_dentry))
  3008. goto exit3;
  3009. /* source must exist */
  3010. error = -ENOENT;
  3011. if (!old_dentry->d_inode)
  3012. goto exit4;
  3013. /* unless the source is a directory trailing slashes give -ENOTDIR */
  3014. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  3015. error = -ENOTDIR;
  3016. if (oldnd.last.name[oldnd.last.len])
  3017. goto exit4;
  3018. if (newnd.last.name[newnd.last.len])
  3019. goto exit4;
  3020. }
  3021. /* source should not be ancestor of target */
  3022. error = -EINVAL;
  3023. if (old_dentry == trap)
  3024. goto exit4;
  3025. new_dentry = lookup_hash(&newnd);
  3026. error = PTR_ERR(new_dentry);
  3027. if (IS_ERR(new_dentry))
  3028. goto exit4;
  3029. /* target should not be an ancestor of source */
  3030. error = -ENOTEMPTY;
  3031. if (new_dentry == trap)
  3032. goto exit5;
  3033. error = mnt_want_write(oldnd.path.mnt);
  3034. if (error)
  3035. goto exit5;
  3036. error = security_path_rename(&oldnd.path, old_dentry,
  3037. &newnd.path, new_dentry);
  3038. if (error)
  3039. goto exit6;
  3040. error = vfs_rename(old_dir->d_inode, old_dentry,
  3041. new_dir->d_inode, new_dentry);
  3042. exit6:
  3043. mnt_drop_write(oldnd.path.mnt);
  3044. exit5:
  3045. dput(new_dentry);
  3046. exit4:
  3047. dput(old_dentry);
  3048. exit3:
  3049. unlock_rename(new_dir, old_dir);
  3050. exit2:
  3051. path_put(&newnd.path);
  3052. putname(to);
  3053. exit1:
  3054. path_put(&oldnd.path);
  3055. putname(from);
  3056. exit:
  3057. return error;
  3058. }
  3059. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  3060. {
  3061. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  3062. }
  3063. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  3064. {
  3065. int len;
  3066. len = PTR_ERR(link);
  3067. if (IS_ERR(link))
  3068. goto out;
  3069. len = strlen(link);
  3070. if (len > (unsigned) buflen)
  3071. len = buflen;
  3072. if (copy_to_user(buffer, link, len))
  3073. len = -EFAULT;
  3074. out:
  3075. return len;
  3076. }
  3077. /*
  3078. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  3079. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  3080. * using) it for any given inode is up to filesystem.
  3081. */
  3082. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3083. {
  3084. struct nameidata nd;
  3085. void *cookie;
  3086. int res;
  3087. nd.depth = 0;
  3088. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  3089. if (IS_ERR(cookie))
  3090. return PTR_ERR(cookie);
  3091. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  3092. if (dentry->d_inode->i_op->put_link)
  3093. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  3094. return res;
  3095. }
  3096. int vfs_follow_link(struct nameidata *nd, const char *link)
  3097. {
  3098. return __vfs_follow_link(nd, link);
  3099. }
  3100. /* get the link contents into pagecache */
  3101. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  3102. {
  3103. char *kaddr;
  3104. struct page *page;
  3105. struct address_space *mapping = dentry->d_inode->i_mapping;
  3106. page = read_mapping_page(mapping, 0, NULL);
  3107. if (IS_ERR(page))
  3108. return (char*)page;
  3109. *ppage = page;
  3110. kaddr = kmap(page);
  3111. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  3112. return kaddr;
  3113. }
  3114. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3115. {
  3116. struct page *page = NULL;
  3117. char *s = page_getlink(dentry, &page);
  3118. int res = vfs_readlink(dentry,buffer,buflen,s);
  3119. if (page) {
  3120. kunmap(page);
  3121. page_cache_release(page);
  3122. }
  3123. return res;
  3124. }
  3125. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  3126. {
  3127. struct page *page = NULL;
  3128. nd_set_link(nd, page_getlink(dentry, &page));
  3129. return page;
  3130. }
  3131. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  3132. {
  3133. struct page *page = cookie;
  3134. if (page) {
  3135. kunmap(page);
  3136. page_cache_release(page);
  3137. }
  3138. }
  3139. /*
  3140. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  3141. */
  3142. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  3143. {
  3144. struct address_space *mapping = inode->i_mapping;
  3145. struct page *page;
  3146. void *fsdata;
  3147. int err;
  3148. char *kaddr;
  3149. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  3150. if (nofs)
  3151. flags |= AOP_FLAG_NOFS;
  3152. retry:
  3153. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  3154. flags, &page, &fsdata);
  3155. if (err)
  3156. goto fail;
  3157. kaddr = kmap_atomic(page, KM_USER0);
  3158. memcpy(kaddr, symname, len-1);
  3159. kunmap_atomic(kaddr, KM_USER0);
  3160. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  3161. page, fsdata);
  3162. if (err < 0)
  3163. goto fail;
  3164. if (err < len-1)
  3165. goto retry;
  3166. mark_inode_dirty(inode);
  3167. return 0;
  3168. fail:
  3169. return err;
  3170. }
  3171. int page_symlink(struct inode *inode, const char *symname, int len)
  3172. {
  3173. return __page_symlink(inode, symname, len,
  3174. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  3175. }
  3176. const struct inode_operations page_symlink_inode_operations = {
  3177. .readlink = generic_readlink,
  3178. .follow_link = page_follow_link_light,
  3179. .put_link = page_put_link,
  3180. };
  3181. EXPORT_SYMBOL(user_path_at);
  3182. EXPORT_SYMBOL(follow_down_one);
  3183. EXPORT_SYMBOL(follow_down);
  3184. EXPORT_SYMBOL(follow_up);
  3185. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  3186. EXPORT_SYMBOL(getname);
  3187. EXPORT_SYMBOL(lock_rename);
  3188. EXPORT_SYMBOL(lookup_one_len);
  3189. EXPORT_SYMBOL(page_follow_link_light);
  3190. EXPORT_SYMBOL(page_put_link);
  3191. EXPORT_SYMBOL(page_readlink);
  3192. EXPORT_SYMBOL(__page_symlink);
  3193. EXPORT_SYMBOL(page_symlink);
  3194. EXPORT_SYMBOL(page_symlink_inode_operations);
  3195. EXPORT_SYMBOL(path_lookup);
  3196. EXPORT_SYMBOL(kern_path);
  3197. EXPORT_SYMBOL(vfs_path_lookup);
  3198. EXPORT_SYMBOL(inode_permission);
  3199. EXPORT_SYMBOL(file_permission);
  3200. EXPORT_SYMBOL(unlock_rename);
  3201. EXPORT_SYMBOL(vfs_create);
  3202. EXPORT_SYMBOL(vfs_follow_link);
  3203. EXPORT_SYMBOL(vfs_link);
  3204. EXPORT_SYMBOL(vfs_mkdir);
  3205. EXPORT_SYMBOL(vfs_mknod);
  3206. EXPORT_SYMBOL(generic_permission);
  3207. EXPORT_SYMBOL(vfs_readlink);
  3208. EXPORT_SYMBOL(vfs_rename);
  3209. EXPORT_SYMBOL(vfs_rmdir);
  3210. EXPORT_SYMBOL(vfs_symlink);
  3211. EXPORT_SYMBOL(vfs_unlink);
  3212. EXPORT_SYMBOL(dentry_unhash);
  3213. EXPORT_SYMBOL(generic_readlink);