namei.c 88 KB

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