namei.c 88 KB

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