namei.c 84 KB

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