namei.c 89 KB

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