namei.c 86 KB

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