namei.c 81 KB

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