namei.c 83 KB

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