namei.c 88 KB

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