namei.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826
  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/quotaops.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/personality.h>
  23. #include <linux/security.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 <asm/namei.h>
  31. #include <asm/uaccess.h>
  32. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  33. /* [Feb-1997 T. Schoebel-Theuer]
  34. * Fundamental changes in the pathname lookup mechanisms (namei)
  35. * were necessary because of omirr. The reason is that omirr needs
  36. * to know the _real_ pathname, not the user-supplied one, in case
  37. * of symlinks (and also when transname replacements occur).
  38. *
  39. * The new code replaces the old recursive symlink resolution with
  40. * an iterative one (in case of non-nested symlink chains). It does
  41. * this with calls to <fs>_follow_link().
  42. * As a side effect, dir_namei(), _namei() and follow_link() are now
  43. * replaced with a single function lookup_dentry() that can handle all
  44. * the special cases of the former code.
  45. *
  46. * With the new dcache, the pathname is stored at each inode, at least as
  47. * long as the refcount of the inode is positive. As a side effect, the
  48. * size of the dcache depends on the inode cache and thus is dynamic.
  49. *
  50. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  51. * resolution to correspond with current state of the code.
  52. *
  53. * Note that the symlink resolution is not *completely* iterative.
  54. * There is still a significant amount of tail- and mid- recursion in
  55. * the algorithm. Also, note that <fs>_readlink() is not used in
  56. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  57. * may return different results than <fs>_follow_link(). Many virtual
  58. * filesystems (including /proc) exhibit this behavior.
  59. */
  60. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  61. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  62. * and the name already exists in form of a symlink, try to create the new
  63. * name indicated by the symlink. The old code always complained that the
  64. * name already exists, due to not following the symlink even if its target
  65. * is nonexistent. The new semantics affects also mknod() and link() when
  66. * the name is a symlink pointing to a non-existant name.
  67. *
  68. * I don't know which semantics is the right one, since I have no access
  69. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  70. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  71. * "old" one. Personally, I think the new semantics is much more logical.
  72. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  73. * file does succeed in both HP-UX and SunOs, but not in Solaris
  74. * and in the old Linux semantics.
  75. */
  76. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  77. * semantics. See the comments in "open_namei" and "do_link" below.
  78. *
  79. * [10-Sep-98 Alan Modra] Another symlink change.
  80. */
  81. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  82. * inside the path - always follow.
  83. * in the last component in creation/removal/renaming - never follow.
  84. * if LOOKUP_FOLLOW passed - follow.
  85. * if the pathname has trailing slashes - follow.
  86. * otherwise - don't follow.
  87. * (applied in that order).
  88. *
  89. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  90. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  91. * During the 2.4 we need to fix the userland stuff depending on it -
  92. * hopefully we will be able to get rid of that wart in 2.5. So far only
  93. * XEmacs seems to be relying on it...
  94. */
  95. /*
  96. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  97. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  98. * any extra contention...
  99. */
  100. static int link_path_walk(const char *name, struct nameidata *nd);
  101. /* In order to reduce some races, while at the same time doing additional
  102. * checking and hopefully speeding things up, we copy filenames to the
  103. * kernel data space before using them..
  104. *
  105. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  106. * PATH_MAX includes the nul terminator --RR.
  107. */
  108. static int do_getname(const char __user *filename, char *page)
  109. {
  110. int retval;
  111. unsigned long len = PATH_MAX;
  112. if (!segment_eq(get_fs(), KERNEL_DS)) {
  113. if ((unsigned long) filename >= TASK_SIZE)
  114. return -EFAULT;
  115. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  116. len = TASK_SIZE - (unsigned long) filename;
  117. }
  118. retval = strncpy_from_user(page, filename, len);
  119. if (retval > 0) {
  120. if (retval < len)
  121. return 0;
  122. return -ENAMETOOLONG;
  123. } else if (!retval)
  124. retval = -ENOENT;
  125. return retval;
  126. }
  127. char * getname(const char __user * filename)
  128. {
  129. char *tmp, *result;
  130. result = ERR_PTR(-ENOMEM);
  131. tmp = __getname();
  132. if (tmp) {
  133. int retval = do_getname(filename, tmp);
  134. result = tmp;
  135. if (retval < 0) {
  136. __putname(tmp);
  137. result = ERR_PTR(retval);
  138. }
  139. }
  140. audit_getname(result);
  141. return result;
  142. }
  143. #ifdef CONFIG_AUDITSYSCALL
  144. void putname(const char *name)
  145. {
  146. if (unlikely(!audit_dummy_context()))
  147. audit_putname(name);
  148. else
  149. __putname(name);
  150. }
  151. EXPORT_SYMBOL(putname);
  152. #endif
  153. /**
  154. * generic_permission - check for access rights on a Posix-like filesystem
  155. * @inode: inode to check access rights for
  156. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  157. * @check_acl: optional callback to check for Posix ACLs
  158. *
  159. * Used to check for read/write/execute permissions on a file.
  160. * We use "fsuid" for this, letting us set arbitrary permissions
  161. * for filesystem access without changing the "normal" uids which
  162. * are used for other things..
  163. */
  164. int generic_permission(struct inode *inode, int mask,
  165. int (*check_acl)(struct inode *inode, int mask))
  166. {
  167. umode_t mode = inode->i_mode;
  168. if (current->fsuid == inode->i_uid)
  169. mode >>= 6;
  170. else {
  171. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  172. int error = check_acl(inode, mask);
  173. if (error == -EACCES)
  174. goto check_capabilities;
  175. else if (error != -EAGAIN)
  176. return error;
  177. }
  178. if (in_group_p(inode->i_gid))
  179. mode >>= 3;
  180. }
  181. /*
  182. * If the DACs are ok we don't need any capability check.
  183. */
  184. if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask))
  185. return 0;
  186. check_capabilities:
  187. /*
  188. * Read/write DACs are always overridable.
  189. * Executable DACs are overridable if at least one exec bit is set.
  190. */
  191. if (!(mask & MAY_EXEC) ||
  192. (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
  193. if (capable(CAP_DAC_OVERRIDE))
  194. return 0;
  195. /*
  196. * Searching includes executable on directories, else just read.
  197. */
  198. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  199. if (capable(CAP_DAC_READ_SEARCH))
  200. return 0;
  201. return -EACCES;
  202. }
  203. int permission(struct inode *inode, int mask, struct nameidata *nd)
  204. {
  205. int retval, submask;
  206. struct vfsmount *mnt = NULL;
  207. if (nd)
  208. mnt = nd->mnt;
  209. if (mask & MAY_WRITE) {
  210. umode_t mode = inode->i_mode;
  211. /*
  212. * Nobody gets write access to a read-only fs.
  213. */
  214. if (IS_RDONLY(inode) &&
  215. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  216. return -EROFS;
  217. /*
  218. * Nobody gets write access to an immutable file.
  219. */
  220. if (IS_IMMUTABLE(inode))
  221. return -EACCES;
  222. }
  223. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  224. /*
  225. * MAY_EXEC on regular files is denied if the fs is mounted
  226. * with the "noexec" flag.
  227. */
  228. if (mnt && (mnt->mnt_flags & MNT_NOEXEC))
  229. return -EACCES;
  230. }
  231. /* Ordinary permission routines do not understand MAY_APPEND. */
  232. submask = mask & ~MAY_APPEND;
  233. if (inode->i_op && inode->i_op->permission) {
  234. retval = inode->i_op->permission(inode, submask, nd);
  235. if (!retval) {
  236. /*
  237. * Exec permission on a regular file is denied if none
  238. * of the execute bits are set.
  239. *
  240. * This check should be done by the ->permission()
  241. * method.
  242. */
  243. if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
  244. !(inode->i_mode & S_IXUGO))
  245. return -EACCES;
  246. }
  247. } else {
  248. retval = generic_permission(inode, submask, NULL);
  249. }
  250. if (retval)
  251. return retval;
  252. return security_inode_permission(inode, mask, nd);
  253. }
  254. /**
  255. * vfs_permission - check for access rights to a given path
  256. * @nd: lookup result that describes the path
  257. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  258. *
  259. * Used to check for read/write/execute permissions on a path.
  260. * We use "fsuid" for this, letting us set arbitrary permissions
  261. * for filesystem access without changing the "normal" uids which
  262. * are used for other things.
  263. */
  264. int vfs_permission(struct nameidata *nd, int mask)
  265. {
  266. return permission(nd->dentry->d_inode, mask, nd);
  267. }
  268. /**
  269. * file_permission - check for additional access rights to a given file
  270. * @file: file to check access rights for
  271. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  272. *
  273. * Used to check for read/write/execute permissions on an already opened
  274. * file.
  275. *
  276. * Note:
  277. * Do not use this function in new code. All access checks should
  278. * be done using vfs_permission().
  279. */
  280. int file_permission(struct file *file, int mask)
  281. {
  282. return permission(file->f_path.dentry->d_inode, mask, NULL);
  283. }
  284. /*
  285. * get_write_access() gets write permission for a file.
  286. * put_write_access() releases this write permission.
  287. * This is used for regular files.
  288. * We cannot support write (and maybe mmap read-write shared) accesses and
  289. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  290. * can have the following values:
  291. * 0: no writers, no VM_DENYWRITE mappings
  292. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  293. * > 0: (i_writecount) users are writing to the file.
  294. *
  295. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  296. * except for the cases where we don't hold i_writecount yet. Then we need to
  297. * use {get,deny}_write_access() - these functions check the sign and refuse
  298. * to do the change if sign is wrong. Exclusion between them is provided by
  299. * the inode->i_lock spinlock.
  300. */
  301. int get_write_access(struct inode * inode)
  302. {
  303. spin_lock(&inode->i_lock);
  304. if (atomic_read(&inode->i_writecount) < 0) {
  305. spin_unlock(&inode->i_lock);
  306. return -ETXTBSY;
  307. }
  308. atomic_inc(&inode->i_writecount);
  309. spin_unlock(&inode->i_lock);
  310. return 0;
  311. }
  312. int deny_write_access(struct file * file)
  313. {
  314. struct inode *inode = file->f_path.dentry->d_inode;
  315. spin_lock(&inode->i_lock);
  316. if (atomic_read(&inode->i_writecount) > 0) {
  317. spin_unlock(&inode->i_lock);
  318. return -ETXTBSY;
  319. }
  320. atomic_dec(&inode->i_writecount);
  321. spin_unlock(&inode->i_lock);
  322. return 0;
  323. }
  324. void path_release(struct nameidata *nd)
  325. {
  326. dput(nd->dentry);
  327. mntput(nd->mnt);
  328. }
  329. /**
  330. * release_open_intent - free up open intent resources
  331. * @nd: pointer to nameidata
  332. */
  333. void release_open_intent(struct nameidata *nd)
  334. {
  335. if (nd->intent.open.file->f_path.dentry == NULL)
  336. put_filp(nd->intent.open.file);
  337. else
  338. fput(nd->intent.open.file);
  339. }
  340. static inline struct dentry *
  341. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  342. {
  343. int status = dentry->d_op->d_revalidate(dentry, nd);
  344. if (unlikely(status <= 0)) {
  345. /*
  346. * The dentry failed validation.
  347. * If d_revalidate returned 0 attempt to invalidate
  348. * the dentry otherwise d_revalidate is asking us
  349. * to return a fail status.
  350. */
  351. if (!status) {
  352. if (!d_invalidate(dentry)) {
  353. dput(dentry);
  354. dentry = NULL;
  355. }
  356. } else {
  357. dput(dentry);
  358. dentry = ERR_PTR(status);
  359. }
  360. }
  361. return dentry;
  362. }
  363. /*
  364. * Internal lookup() using the new generic dcache.
  365. * SMP-safe
  366. */
  367. static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  368. {
  369. struct dentry * dentry = __d_lookup(parent, name);
  370. /* lockess __d_lookup may fail due to concurrent d_move()
  371. * in some unrelated directory, so try with d_lookup
  372. */
  373. if (!dentry)
  374. dentry = d_lookup(parent, name);
  375. if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
  376. dentry = do_revalidate(dentry, nd);
  377. return dentry;
  378. }
  379. /*
  380. * Short-cut version of permission(), for calling by
  381. * path_walk(), when dcache lock is held. Combines parts
  382. * of permission() and generic_permission(), and tests ONLY for
  383. * MAY_EXEC permission.
  384. *
  385. * If appropriate, check DAC only. If not appropriate, or
  386. * short-cut DAC fails, then call permission() to do more
  387. * complete permission check.
  388. */
  389. static int exec_permission_lite(struct inode *inode,
  390. struct nameidata *nd)
  391. {
  392. umode_t mode = inode->i_mode;
  393. if (inode->i_op && inode->i_op->permission)
  394. return -EAGAIN;
  395. if (current->fsuid == inode->i_uid)
  396. mode >>= 6;
  397. else if (in_group_p(inode->i_gid))
  398. mode >>= 3;
  399. if (mode & MAY_EXEC)
  400. goto ok;
  401. if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
  402. goto ok;
  403. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
  404. goto ok;
  405. if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
  406. goto ok;
  407. return -EACCES;
  408. ok:
  409. return security_inode_permission(inode, MAY_EXEC, nd);
  410. }
  411. /*
  412. * This is called when everything else fails, and we actually have
  413. * to go to the low-level filesystem to find out what we should do..
  414. *
  415. * We get the directory semaphore, and after getting that we also
  416. * make sure that nobody added the entry to the dcache in the meantime..
  417. * SMP-safe
  418. */
  419. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  420. {
  421. struct dentry * result;
  422. struct inode *dir = parent->d_inode;
  423. mutex_lock(&dir->i_mutex);
  424. /*
  425. * First re-do the cached lookup just in case it was created
  426. * while we waited for the directory semaphore..
  427. *
  428. * FIXME! This could use version numbering or similar to
  429. * avoid unnecessary cache lookups.
  430. *
  431. * The "dcache_lock" is purely to protect the RCU list walker
  432. * from concurrent renames at this point (we mustn't get false
  433. * negatives from the RCU list walk here, unlike the optimistic
  434. * fast walk).
  435. *
  436. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  437. */
  438. result = d_lookup(parent, name);
  439. if (!result) {
  440. struct dentry * dentry = d_alloc(parent, name);
  441. result = ERR_PTR(-ENOMEM);
  442. if (dentry) {
  443. result = dir->i_op->lookup(dir, dentry, nd);
  444. if (result)
  445. dput(dentry);
  446. else
  447. result = dentry;
  448. }
  449. mutex_unlock(&dir->i_mutex);
  450. return result;
  451. }
  452. /*
  453. * Uhhuh! Nasty case: the cache was re-populated while
  454. * we waited on the semaphore. Need to revalidate.
  455. */
  456. mutex_unlock(&dir->i_mutex);
  457. if (result->d_op && result->d_op->d_revalidate) {
  458. result = do_revalidate(result, nd);
  459. if (!result)
  460. result = ERR_PTR(-ENOENT);
  461. }
  462. return result;
  463. }
  464. static int __emul_lookup_dentry(const char *, struct nameidata *);
  465. /* SMP-safe */
  466. static __always_inline int
  467. walk_init_root(const char *name, struct nameidata *nd)
  468. {
  469. struct fs_struct *fs = current->fs;
  470. read_lock(&fs->lock);
  471. if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  472. nd->mnt = mntget(fs->altrootmnt);
  473. nd->dentry = dget(fs->altroot);
  474. read_unlock(&fs->lock);
  475. if (__emul_lookup_dentry(name,nd))
  476. return 0;
  477. read_lock(&fs->lock);
  478. }
  479. nd->mnt = mntget(fs->rootmnt);
  480. nd->dentry = dget(fs->root);
  481. read_unlock(&fs->lock);
  482. return 1;
  483. }
  484. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  485. {
  486. int res = 0;
  487. char *name;
  488. if (IS_ERR(link))
  489. goto fail;
  490. if (*link == '/') {
  491. path_release(nd);
  492. if (!walk_init_root(link, nd))
  493. /* weird __emul_prefix() stuff did it */
  494. goto out;
  495. }
  496. res = link_path_walk(link, nd);
  497. out:
  498. if (nd->depth || res || nd->last_type!=LAST_NORM)
  499. return res;
  500. /*
  501. * If it is an iterative symlinks resolution in open_namei() we
  502. * have to copy the last component. And all that crap because of
  503. * bloody create() on broken symlinks. Furrfu...
  504. */
  505. name = __getname();
  506. if (unlikely(!name)) {
  507. path_release(nd);
  508. return -ENOMEM;
  509. }
  510. strcpy(name, nd->last.name);
  511. nd->last.name = name;
  512. return 0;
  513. fail:
  514. path_release(nd);
  515. return PTR_ERR(link);
  516. }
  517. static inline void dput_path(struct path *path, struct nameidata *nd)
  518. {
  519. dput(path->dentry);
  520. if (path->mnt != nd->mnt)
  521. mntput(path->mnt);
  522. }
  523. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  524. {
  525. dput(nd->dentry);
  526. if (nd->mnt != path->mnt)
  527. mntput(nd->mnt);
  528. nd->mnt = path->mnt;
  529. nd->dentry = path->dentry;
  530. }
  531. static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
  532. {
  533. int error;
  534. void *cookie;
  535. struct dentry *dentry = path->dentry;
  536. touch_atime(path->mnt, dentry);
  537. nd_set_link(nd, NULL);
  538. if (path->mnt != nd->mnt) {
  539. path_to_nameidata(path, nd);
  540. dget(dentry);
  541. }
  542. mntget(path->mnt);
  543. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  544. error = PTR_ERR(cookie);
  545. if (!IS_ERR(cookie)) {
  546. char *s = nd_get_link(nd);
  547. error = 0;
  548. if (s)
  549. error = __vfs_follow_link(nd, s);
  550. if (dentry->d_inode->i_op->put_link)
  551. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  552. }
  553. dput(dentry);
  554. mntput(path->mnt);
  555. return error;
  556. }
  557. /*
  558. * This limits recursive symlink follows to 8, while
  559. * limiting consecutive symlinks to 40.
  560. *
  561. * Without that kind of total limit, nasty chains of consecutive
  562. * symlinks can cause almost arbitrarily long lookups.
  563. */
  564. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  565. {
  566. int err = -ELOOP;
  567. if (current->link_count >= MAX_NESTED_LINKS)
  568. goto loop;
  569. if (current->total_link_count >= 40)
  570. goto loop;
  571. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  572. cond_resched();
  573. err = security_inode_follow_link(path->dentry, nd);
  574. if (err)
  575. goto loop;
  576. current->link_count++;
  577. current->total_link_count++;
  578. nd->depth++;
  579. err = __do_follow_link(path, nd);
  580. current->link_count--;
  581. nd->depth--;
  582. return err;
  583. loop:
  584. dput_path(path, nd);
  585. path_release(nd);
  586. return err;
  587. }
  588. int follow_up(struct vfsmount **mnt, struct dentry **dentry)
  589. {
  590. struct vfsmount *parent;
  591. struct dentry *mountpoint;
  592. spin_lock(&vfsmount_lock);
  593. parent=(*mnt)->mnt_parent;
  594. if (parent == *mnt) {
  595. spin_unlock(&vfsmount_lock);
  596. return 0;
  597. }
  598. mntget(parent);
  599. mountpoint=dget((*mnt)->mnt_mountpoint);
  600. spin_unlock(&vfsmount_lock);
  601. dput(*dentry);
  602. *dentry = mountpoint;
  603. mntput(*mnt);
  604. *mnt = parent;
  605. return 1;
  606. }
  607. /* no need for dcache_lock, as serialization is taken care in
  608. * namespace.c
  609. */
  610. static int __follow_mount(struct path *path)
  611. {
  612. int res = 0;
  613. while (d_mountpoint(path->dentry)) {
  614. struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
  615. if (!mounted)
  616. break;
  617. dput(path->dentry);
  618. if (res)
  619. mntput(path->mnt);
  620. path->mnt = mounted;
  621. path->dentry = dget(mounted->mnt_root);
  622. res = 1;
  623. }
  624. return res;
  625. }
  626. static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
  627. {
  628. while (d_mountpoint(*dentry)) {
  629. struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
  630. if (!mounted)
  631. break;
  632. dput(*dentry);
  633. mntput(*mnt);
  634. *mnt = mounted;
  635. *dentry = dget(mounted->mnt_root);
  636. }
  637. }
  638. /* no need for dcache_lock, as serialization is taken care in
  639. * namespace.c
  640. */
  641. int follow_down(struct vfsmount **mnt, struct dentry **dentry)
  642. {
  643. struct vfsmount *mounted;
  644. mounted = lookup_mnt(*mnt, *dentry);
  645. if (mounted) {
  646. dput(*dentry);
  647. mntput(*mnt);
  648. *mnt = mounted;
  649. *dentry = dget(mounted->mnt_root);
  650. return 1;
  651. }
  652. return 0;
  653. }
  654. static __always_inline void follow_dotdot(struct nameidata *nd)
  655. {
  656. struct fs_struct *fs = current->fs;
  657. while(1) {
  658. struct vfsmount *parent;
  659. struct dentry *old = nd->dentry;
  660. read_lock(&fs->lock);
  661. if (nd->dentry == fs->root &&
  662. nd->mnt == fs->rootmnt) {
  663. read_unlock(&fs->lock);
  664. break;
  665. }
  666. read_unlock(&fs->lock);
  667. spin_lock(&dcache_lock);
  668. if (nd->dentry != nd->mnt->mnt_root) {
  669. nd->dentry = dget(nd->dentry->d_parent);
  670. spin_unlock(&dcache_lock);
  671. dput(old);
  672. break;
  673. }
  674. spin_unlock(&dcache_lock);
  675. spin_lock(&vfsmount_lock);
  676. parent = nd->mnt->mnt_parent;
  677. if (parent == nd->mnt) {
  678. spin_unlock(&vfsmount_lock);
  679. break;
  680. }
  681. mntget(parent);
  682. nd->dentry = dget(nd->mnt->mnt_mountpoint);
  683. spin_unlock(&vfsmount_lock);
  684. dput(old);
  685. mntput(nd->mnt);
  686. nd->mnt = parent;
  687. }
  688. follow_mount(&nd->mnt, &nd->dentry);
  689. }
  690. /*
  691. * It's more convoluted than I'd like it to be, but... it's still fairly
  692. * small and for now I'd prefer to have fast path as straight as possible.
  693. * It _is_ time-critical.
  694. */
  695. static int do_lookup(struct nameidata *nd, struct qstr *name,
  696. struct path *path)
  697. {
  698. struct vfsmount *mnt = nd->mnt;
  699. struct dentry *dentry = __d_lookup(nd->dentry, name);
  700. if (!dentry)
  701. goto need_lookup;
  702. if (dentry->d_op && dentry->d_op->d_revalidate)
  703. goto need_revalidate;
  704. done:
  705. path->mnt = mnt;
  706. path->dentry = dentry;
  707. __follow_mount(path);
  708. return 0;
  709. need_lookup:
  710. dentry = real_lookup(nd->dentry, name, nd);
  711. if (IS_ERR(dentry))
  712. goto fail;
  713. goto done;
  714. need_revalidate:
  715. dentry = do_revalidate(dentry, nd);
  716. if (!dentry)
  717. goto need_lookup;
  718. if (IS_ERR(dentry))
  719. goto fail;
  720. goto done;
  721. fail:
  722. return PTR_ERR(dentry);
  723. }
  724. /*
  725. * Name resolution.
  726. * This is the basic name resolution function, turning a pathname into
  727. * the final dentry. We expect 'base' to be positive and a directory.
  728. *
  729. * Returns 0 and nd will have valid dentry and mnt on success.
  730. * Returns error and drops reference to input namei data on failure.
  731. */
  732. static int __link_path_walk(const char *name, struct nameidata *nd)
  733. {
  734. struct path next;
  735. struct inode *inode;
  736. int err;
  737. unsigned int lookup_flags = nd->flags;
  738. while (*name=='/')
  739. name++;
  740. if (!*name)
  741. goto return_reval;
  742. inode = nd->dentry->d_inode;
  743. if (nd->depth)
  744. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  745. /* At this point we know we have a real path component. */
  746. for(;;) {
  747. unsigned long hash;
  748. struct qstr this;
  749. unsigned int c;
  750. nd->flags |= LOOKUP_CONTINUE;
  751. err = exec_permission_lite(inode, nd);
  752. if (err == -EAGAIN)
  753. err = vfs_permission(nd, MAY_EXEC);
  754. if (err)
  755. break;
  756. this.name = name;
  757. c = *(const unsigned char *)name;
  758. hash = init_name_hash();
  759. do {
  760. name++;
  761. hash = partial_name_hash(c, hash);
  762. c = *(const unsigned char *)name;
  763. } while (c && (c != '/'));
  764. this.len = name - (const char *) this.name;
  765. this.hash = end_name_hash(hash);
  766. /* remove trailing slashes? */
  767. if (!c)
  768. goto last_component;
  769. while (*++name == '/');
  770. if (!*name)
  771. goto last_with_slashes;
  772. /*
  773. * "." and ".." are special - ".." especially so because it has
  774. * to be able to know about the current root directory and
  775. * parent relationships.
  776. */
  777. if (this.name[0] == '.') switch (this.len) {
  778. default:
  779. break;
  780. case 2:
  781. if (this.name[1] != '.')
  782. break;
  783. follow_dotdot(nd);
  784. inode = nd->dentry->d_inode;
  785. /* fallthrough */
  786. case 1:
  787. continue;
  788. }
  789. /*
  790. * See if the low-level filesystem might want
  791. * to use its own hash..
  792. */
  793. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  794. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  795. if (err < 0)
  796. break;
  797. }
  798. /* This does the actual lookups.. */
  799. err = do_lookup(nd, &this, &next);
  800. if (err)
  801. break;
  802. err = -ENOENT;
  803. inode = next.dentry->d_inode;
  804. if (!inode)
  805. goto out_dput;
  806. err = -ENOTDIR;
  807. if (!inode->i_op)
  808. goto out_dput;
  809. if (inode->i_op->follow_link) {
  810. err = do_follow_link(&next, nd);
  811. if (err)
  812. goto return_err;
  813. err = -ENOENT;
  814. inode = nd->dentry->d_inode;
  815. if (!inode)
  816. break;
  817. err = -ENOTDIR;
  818. if (!inode->i_op)
  819. break;
  820. } else
  821. path_to_nameidata(&next, nd);
  822. err = -ENOTDIR;
  823. if (!inode->i_op->lookup)
  824. break;
  825. continue;
  826. /* here ends the main loop */
  827. last_with_slashes:
  828. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  829. last_component:
  830. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  831. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  832. if (lookup_flags & LOOKUP_PARENT)
  833. goto lookup_parent;
  834. if (this.name[0] == '.') switch (this.len) {
  835. default:
  836. break;
  837. case 2:
  838. if (this.name[1] != '.')
  839. break;
  840. follow_dotdot(nd);
  841. inode = nd->dentry->d_inode;
  842. /* fallthrough */
  843. case 1:
  844. goto return_reval;
  845. }
  846. if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {
  847. err = nd->dentry->d_op->d_hash(nd->dentry, &this);
  848. if (err < 0)
  849. break;
  850. }
  851. err = do_lookup(nd, &this, &next);
  852. if (err)
  853. break;
  854. inode = next.dentry->d_inode;
  855. if ((lookup_flags & LOOKUP_FOLLOW)
  856. && inode && inode->i_op && inode->i_op->follow_link) {
  857. err = do_follow_link(&next, nd);
  858. if (err)
  859. goto return_err;
  860. inode = nd->dentry->d_inode;
  861. } else
  862. path_to_nameidata(&next, nd);
  863. err = -ENOENT;
  864. if (!inode)
  865. break;
  866. if (lookup_flags & LOOKUP_DIRECTORY) {
  867. err = -ENOTDIR;
  868. if (!inode->i_op || !inode->i_op->lookup)
  869. break;
  870. }
  871. goto return_base;
  872. lookup_parent:
  873. nd->last = this;
  874. nd->last_type = LAST_NORM;
  875. if (this.name[0] != '.')
  876. goto return_base;
  877. if (this.len == 1)
  878. nd->last_type = LAST_DOT;
  879. else if (this.len == 2 && this.name[1] == '.')
  880. nd->last_type = LAST_DOTDOT;
  881. else
  882. goto return_base;
  883. return_reval:
  884. /*
  885. * We bypassed the ordinary revalidation routines.
  886. * We may need to check the cached dentry for staleness.
  887. */
  888. if (nd->dentry && nd->dentry->d_sb &&
  889. (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  890. err = -ESTALE;
  891. /* Note: we do not d_invalidate() */
  892. if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd))
  893. break;
  894. }
  895. return_base:
  896. return 0;
  897. out_dput:
  898. dput_path(&next, nd);
  899. break;
  900. }
  901. path_release(nd);
  902. return_err:
  903. return err;
  904. }
  905. /*
  906. * Wrapper to retry pathname resolution whenever the underlying
  907. * file system returns an ESTALE.
  908. *
  909. * Retry the whole path once, forcing real lookup requests
  910. * instead of relying on the dcache.
  911. */
  912. static int link_path_walk(const char *name, struct nameidata *nd)
  913. {
  914. struct nameidata save = *nd;
  915. int result;
  916. /* make sure the stuff we saved doesn't go away */
  917. dget(save.dentry);
  918. mntget(save.mnt);
  919. result = __link_path_walk(name, nd);
  920. if (result == -ESTALE) {
  921. *nd = save;
  922. dget(nd->dentry);
  923. mntget(nd->mnt);
  924. nd->flags |= LOOKUP_REVAL;
  925. result = __link_path_walk(name, nd);
  926. }
  927. dput(save.dentry);
  928. mntput(save.mnt);
  929. return result;
  930. }
  931. static int path_walk(const char *name, struct nameidata *nd)
  932. {
  933. current->total_link_count = 0;
  934. return link_path_walk(name, nd);
  935. }
  936. /*
  937. * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
  938. * everything is done. Returns 0 and drops input nd, if lookup failed;
  939. */
  940. static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
  941. {
  942. if (path_walk(name, nd))
  943. return 0; /* something went wrong... */
  944. if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) {
  945. struct dentry *old_dentry = nd->dentry;
  946. struct vfsmount *old_mnt = nd->mnt;
  947. struct qstr last = nd->last;
  948. int last_type = nd->last_type;
  949. struct fs_struct *fs = current->fs;
  950. /*
  951. * NAME was not found in alternate root or it's a directory.
  952. * Try to find it in the normal root:
  953. */
  954. nd->last_type = LAST_ROOT;
  955. read_lock(&fs->lock);
  956. nd->mnt = mntget(fs->rootmnt);
  957. nd->dentry = dget(fs->root);
  958. read_unlock(&fs->lock);
  959. if (path_walk(name, nd) == 0) {
  960. if (nd->dentry->d_inode) {
  961. dput(old_dentry);
  962. mntput(old_mnt);
  963. return 1;
  964. }
  965. path_release(nd);
  966. }
  967. nd->dentry = old_dentry;
  968. nd->mnt = old_mnt;
  969. nd->last = last;
  970. nd->last_type = last_type;
  971. }
  972. return 1;
  973. }
  974. void set_fs_altroot(void)
  975. {
  976. char *emul = __emul_prefix();
  977. struct nameidata nd;
  978. struct vfsmount *mnt = NULL, *oldmnt;
  979. struct dentry *dentry = NULL, *olddentry;
  980. int err;
  981. struct fs_struct *fs = current->fs;
  982. if (!emul)
  983. goto set_it;
  984. err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  985. if (!err) {
  986. mnt = nd.mnt;
  987. dentry = nd.dentry;
  988. }
  989. set_it:
  990. write_lock(&fs->lock);
  991. oldmnt = fs->altrootmnt;
  992. olddentry = fs->altroot;
  993. fs->altrootmnt = mnt;
  994. fs->altroot = dentry;
  995. write_unlock(&fs->lock);
  996. if (olddentry) {
  997. dput(olddentry);
  998. mntput(oldmnt);
  999. }
  1000. }
  1001. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1002. static int do_path_lookup(int dfd, const char *name,
  1003. unsigned int flags, struct nameidata *nd)
  1004. {
  1005. int retval = 0;
  1006. int fput_needed;
  1007. struct file *file;
  1008. struct fs_struct *fs = current->fs;
  1009. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1010. nd->flags = flags;
  1011. nd->depth = 0;
  1012. if (*name=='/') {
  1013. read_lock(&fs->lock);
  1014. if (fs->altroot && !(nd->flags & LOOKUP_NOALT)) {
  1015. nd->mnt = mntget(fs->altrootmnt);
  1016. nd->dentry = dget(fs->altroot);
  1017. read_unlock(&fs->lock);
  1018. if (__emul_lookup_dentry(name,nd))
  1019. goto out; /* found in altroot */
  1020. read_lock(&fs->lock);
  1021. }
  1022. nd->mnt = mntget(fs->rootmnt);
  1023. nd->dentry = dget(fs->root);
  1024. read_unlock(&fs->lock);
  1025. } else if (dfd == AT_FDCWD) {
  1026. read_lock(&fs->lock);
  1027. nd->mnt = mntget(fs->pwdmnt);
  1028. nd->dentry = dget(fs->pwd);
  1029. read_unlock(&fs->lock);
  1030. } else {
  1031. struct dentry *dentry;
  1032. file = fget_light(dfd, &fput_needed);
  1033. retval = -EBADF;
  1034. if (!file)
  1035. goto out_fail;
  1036. dentry = file->f_path.dentry;
  1037. retval = -ENOTDIR;
  1038. if (!S_ISDIR(dentry->d_inode->i_mode))
  1039. goto fput_fail;
  1040. retval = file_permission(file, MAY_EXEC);
  1041. if (retval)
  1042. goto fput_fail;
  1043. nd->mnt = mntget(file->f_path.mnt);
  1044. nd->dentry = dget(dentry);
  1045. fput_light(file, fput_needed);
  1046. }
  1047. retval = path_walk(name, nd);
  1048. out:
  1049. if (unlikely(!retval && !audit_dummy_context() && nd->dentry &&
  1050. nd->dentry->d_inode))
  1051. audit_inode(name, nd->dentry);
  1052. out_fail:
  1053. return retval;
  1054. fput_fail:
  1055. fput_light(file, fput_needed);
  1056. goto out_fail;
  1057. }
  1058. int path_lookup(const char *name, unsigned int flags,
  1059. struct nameidata *nd)
  1060. {
  1061. return do_path_lookup(AT_FDCWD, name, flags, nd);
  1062. }
  1063. /**
  1064. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1065. * @dentry: pointer to dentry of the base directory
  1066. * @mnt: pointer to vfs mount of the base directory
  1067. * @name: pointer to file name
  1068. * @flags: lookup flags
  1069. * @nd: pointer to nameidata
  1070. */
  1071. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1072. const char *name, unsigned int flags,
  1073. struct nameidata *nd)
  1074. {
  1075. int retval;
  1076. /* same as do_path_lookup */
  1077. nd->last_type = LAST_ROOT;
  1078. nd->flags = flags;
  1079. nd->depth = 0;
  1080. nd->mnt = mntget(mnt);
  1081. nd->dentry = dget(dentry);
  1082. retval = path_walk(name, nd);
  1083. if (unlikely(!retval && !audit_dummy_context() && nd->dentry &&
  1084. nd->dentry->d_inode))
  1085. audit_inode(name, nd->dentry);
  1086. return retval;
  1087. }
  1088. static int __path_lookup_intent_open(int dfd, const char *name,
  1089. unsigned int lookup_flags, struct nameidata *nd,
  1090. int open_flags, int create_mode)
  1091. {
  1092. struct file *filp = get_empty_filp();
  1093. int err;
  1094. if (filp == NULL)
  1095. return -ENFILE;
  1096. nd->intent.open.file = filp;
  1097. nd->intent.open.flags = open_flags;
  1098. nd->intent.open.create_mode = create_mode;
  1099. err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
  1100. if (IS_ERR(nd->intent.open.file)) {
  1101. if (err == 0) {
  1102. err = PTR_ERR(nd->intent.open.file);
  1103. path_release(nd);
  1104. }
  1105. } else if (err != 0)
  1106. release_open_intent(nd);
  1107. return err;
  1108. }
  1109. /**
  1110. * path_lookup_open - lookup a file path with open intent
  1111. * @dfd: the directory to use as base, or AT_FDCWD
  1112. * @name: pointer to file name
  1113. * @lookup_flags: lookup intent flags
  1114. * @nd: pointer to nameidata
  1115. * @open_flags: open intent flags
  1116. */
  1117. int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
  1118. struct nameidata *nd, int open_flags)
  1119. {
  1120. return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
  1121. open_flags, 0);
  1122. }
  1123. /**
  1124. * path_lookup_create - lookup a file path with open + create intent
  1125. * @dfd: the directory to use as base, or AT_FDCWD
  1126. * @name: pointer to file name
  1127. * @lookup_flags: lookup intent flags
  1128. * @nd: pointer to nameidata
  1129. * @open_flags: open intent flags
  1130. * @create_mode: create intent flags
  1131. */
  1132. static int path_lookup_create(int dfd, const char *name,
  1133. unsigned int lookup_flags, struct nameidata *nd,
  1134. int open_flags, int create_mode)
  1135. {
  1136. return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
  1137. nd, open_flags, create_mode);
  1138. }
  1139. int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
  1140. struct nameidata *nd, int open_flags)
  1141. {
  1142. char *tmp = getname(name);
  1143. int err = PTR_ERR(tmp);
  1144. if (!IS_ERR(tmp)) {
  1145. err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
  1146. putname(tmp);
  1147. }
  1148. return err;
  1149. }
  1150. static struct dentry *__lookup_hash(struct qstr *name,
  1151. struct dentry *base, struct nameidata *nd)
  1152. {
  1153. struct dentry *dentry;
  1154. struct inode *inode;
  1155. int err;
  1156. inode = base->d_inode;
  1157. /*
  1158. * See if the low-level filesystem might want
  1159. * to use its own hash..
  1160. */
  1161. if (base->d_op && base->d_op->d_hash) {
  1162. err = base->d_op->d_hash(base, name);
  1163. dentry = ERR_PTR(err);
  1164. if (err < 0)
  1165. goto out;
  1166. }
  1167. dentry = cached_lookup(base, name, nd);
  1168. if (!dentry) {
  1169. struct dentry *new = d_alloc(base, name);
  1170. dentry = ERR_PTR(-ENOMEM);
  1171. if (!new)
  1172. goto out;
  1173. dentry = inode->i_op->lookup(inode, new, nd);
  1174. if (!dentry)
  1175. dentry = new;
  1176. else
  1177. dput(new);
  1178. }
  1179. out:
  1180. return dentry;
  1181. }
  1182. /*
  1183. * Restricted form of lookup. Doesn't follow links, single-component only,
  1184. * needs parent already locked. Doesn't follow mounts.
  1185. * SMP-safe.
  1186. */
  1187. static struct dentry *lookup_hash(struct nameidata *nd)
  1188. {
  1189. int err;
  1190. err = permission(nd->dentry->d_inode, MAY_EXEC, nd);
  1191. if (err)
  1192. return ERR_PTR(err);
  1193. return __lookup_hash(&nd->last, nd->dentry, nd);
  1194. }
  1195. static int __lookup_one_len(const char *name, struct qstr *this,
  1196. struct dentry *base, int len)
  1197. {
  1198. unsigned long hash;
  1199. unsigned int c;
  1200. this->name = name;
  1201. this->len = len;
  1202. if (!len)
  1203. return -EACCES;
  1204. hash = init_name_hash();
  1205. while (len--) {
  1206. c = *(const unsigned char *)name++;
  1207. if (c == '/' || c == '\0')
  1208. return -EACCES;
  1209. hash = partial_name_hash(c, hash);
  1210. }
  1211. this->hash = end_name_hash(hash);
  1212. return 0;
  1213. }
  1214. /**
  1215. * lookup_one_len: filesystem helper to lookup single pathname component
  1216. * @name: pathname component to lookup
  1217. * @base: base directory to lookup from
  1218. * @len: maximum length @len should be interpreted to
  1219. *
  1220. * Note that this routine is purely a helper for filesystem useage and should
  1221. * not be called by generic code. Also note that by using this function to
  1222. * nameidata argument is passed to the filesystem methods and a filesystem
  1223. * using this helper needs to be prepared for that.
  1224. */
  1225. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1226. {
  1227. int err;
  1228. struct qstr this;
  1229. err = __lookup_one_len(name, &this, base, len);
  1230. if (err)
  1231. return ERR_PTR(err);
  1232. err = permission(base->d_inode, MAY_EXEC, NULL);
  1233. if (err)
  1234. return ERR_PTR(err);
  1235. return __lookup_hash(&this, base, NULL);
  1236. }
  1237. /**
  1238. * lookup_one_noperm - bad hack for sysfs
  1239. * @name: pathname component to lookup
  1240. * @base: base directory to lookup from
  1241. *
  1242. * This is a variant of lookup_one_len that doesn't perform any permission
  1243. * checks. It's a horrible hack to work around the braindead sysfs
  1244. * architecture and should not be used anywhere else.
  1245. *
  1246. * DON'T USE THIS FUNCTION EVER, thanks.
  1247. */
  1248. struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
  1249. {
  1250. int err;
  1251. struct qstr this;
  1252. err = __lookup_one_len(name, &this, base, strlen(name));
  1253. if (err)
  1254. return ERR_PTR(err);
  1255. return __lookup_hash(&this, base, NULL);
  1256. }
  1257. int __user_walk_fd(int dfd, const char __user *name, unsigned flags,
  1258. struct nameidata *nd)
  1259. {
  1260. char *tmp = getname(name);
  1261. int err = PTR_ERR(tmp);
  1262. if (!IS_ERR(tmp)) {
  1263. err = do_path_lookup(dfd, tmp, flags, nd);
  1264. putname(tmp);
  1265. }
  1266. return err;
  1267. }
  1268. int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
  1269. {
  1270. return __user_walk_fd(AT_FDCWD, name, flags, nd);
  1271. }
  1272. /*
  1273. * It's inline, so penalty for filesystems that don't use sticky bit is
  1274. * minimal.
  1275. */
  1276. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1277. {
  1278. if (!(dir->i_mode & S_ISVTX))
  1279. return 0;
  1280. if (inode->i_uid == current->fsuid)
  1281. return 0;
  1282. if (dir->i_uid == current->fsuid)
  1283. return 0;
  1284. return !capable(CAP_FOWNER);
  1285. }
  1286. /*
  1287. * Check whether we can remove a link victim from directory dir, check
  1288. * whether the type of victim is right.
  1289. * 1. We can't do it if dir is read-only (done in permission())
  1290. * 2. We should have write and exec permissions on dir
  1291. * 3. We can't remove anything from append-only dir
  1292. * 4. We can't do anything with immutable dir (done in permission())
  1293. * 5. If the sticky bit on dir is set we should either
  1294. * a. be owner of dir, or
  1295. * b. be owner of victim, or
  1296. * c. have CAP_FOWNER capability
  1297. * 6. If the victim is append-only or immutable we can't do antyhing with
  1298. * links pointing to it.
  1299. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1300. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1301. * 9. We can't remove a root or mountpoint.
  1302. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1303. * nfs_async_unlink().
  1304. */
  1305. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1306. {
  1307. int error;
  1308. if (!victim->d_inode)
  1309. return -ENOENT;
  1310. BUG_ON(victim->d_parent->d_inode != dir);
  1311. audit_inode_child(victim->d_name.name, victim, dir);
  1312. error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
  1313. if (error)
  1314. return error;
  1315. if (IS_APPEND(dir))
  1316. return -EPERM;
  1317. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1318. IS_IMMUTABLE(victim->d_inode))
  1319. return -EPERM;
  1320. if (isdir) {
  1321. if (!S_ISDIR(victim->d_inode->i_mode))
  1322. return -ENOTDIR;
  1323. if (IS_ROOT(victim))
  1324. return -EBUSY;
  1325. } else if (S_ISDIR(victim->d_inode->i_mode))
  1326. return -EISDIR;
  1327. if (IS_DEADDIR(dir))
  1328. return -ENOENT;
  1329. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1330. return -EBUSY;
  1331. return 0;
  1332. }
  1333. /* Check whether we can create an object with dentry child in directory
  1334. * dir.
  1335. * 1. We can't do it if child already exists (open has special treatment for
  1336. * this case, but since we are inlined it's OK)
  1337. * 2. We can't do it if dir is read-only (done in permission())
  1338. * 3. We should have write and exec permissions on dir
  1339. * 4. We can't do it if dir is immutable (done in permission())
  1340. */
  1341. static inline int may_create(struct inode *dir, struct dentry *child,
  1342. struct nameidata *nd)
  1343. {
  1344. if (child->d_inode)
  1345. return -EEXIST;
  1346. if (IS_DEADDIR(dir))
  1347. return -ENOENT;
  1348. return permission(dir,MAY_WRITE | MAY_EXEC, nd);
  1349. }
  1350. /*
  1351. * O_DIRECTORY translates into forcing a directory lookup.
  1352. */
  1353. static inline int lookup_flags(unsigned int f)
  1354. {
  1355. unsigned long retval = LOOKUP_FOLLOW;
  1356. if (f & O_NOFOLLOW)
  1357. retval &= ~LOOKUP_FOLLOW;
  1358. if (f & O_DIRECTORY)
  1359. retval |= LOOKUP_DIRECTORY;
  1360. return retval;
  1361. }
  1362. /*
  1363. * p1 and p2 should be directories on the same fs.
  1364. */
  1365. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1366. {
  1367. struct dentry *p;
  1368. if (p1 == p2) {
  1369. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1370. return NULL;
  1371. }
  1372. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1373. for (p = p1; p->d_parent != p; p = p->d_parent) {
  1374. if (p->d_parent == p2) {
  1375. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1376. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1377. return p;
  1378. }
  1379. }
  1380. for (p = p2; p->d_parent != p; p = p->d_parent) {
  1381. if (p->d_parent == p1) {
  1382. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1383. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1384. return p;
  1385. }
  1386. }
  1387. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1388. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1389. return NULL;
  1390. }
  1391. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1392. {
  1393. mutex_unlock(&p1->d_inode->i_mutex);
  1394. if (p1 != p2) {
  1395. mutex_unlock(&p2->d_inode->i_mutex);
  1396. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1397. }
  1398. }
  1399. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1400. struct nameidata *nd)
  1401. {
  1402. int error = may_create(dir, dentry, nd);
  1403. if (error)
  1404. return error;
  1405. if (!dir->i_op || !dir->i_op->create)
  1406. return -EACCES; /* shouldn't it be ENOSYS? */
  1407. mode &= S_IALLUGO;
  1408. mode |= S_IFREG;
  1409. error = security_inode_create(dir, dentry, mode);
  1410. if (error)
  1411. return error;
  1412. DQUOT_INIT(dir);
  1413. error = dir->i_op->create(dir, dentry, mode, nd);
  1414. if (!error)
  1415. fsnotify_create(dir, dentry);
  1416. return error;
  1417. }
  1418. int may_open(struct nameidata *nd, int acc_mode, int flag)
  1419. {
  1420. struct dentry *dentry = nd->dentry;
  1421. struct inode *inode = dentry->d_inode;
  1422. int error;
  1423. if (!inode)
  1424. return -ENOENT;
  1425. if (S_ISLNK(inode->i_mode))
  1426. return -ELOOP;
  1427. if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
  1428. return -EISDIR;
  1429. /*
  1430. * FIFO's, sockets and device files are special: they don't
  1431. * actually live on the filesystem itself, and as such you
  1432. * can write to them even if the filesystem is read-only.
  1433. */
  1434. if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  1435. flag &= ~O_TRUNC;
  1436. } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
  1437. if (nd->mnt->mnt_flags & MNT_NODEV)
  1438. return -EACCES;
  1439. flag &= ~O_TRUNC;
  1440. } else if (IS_RDONLY(inode) && (acc_mode & MAY_WRITE))
  1441. return -EROFS;
  1442. error = vfs_permission(nd, acc_mode);
  1443. if (error)
  1444. return error;
  1445. /*
  1446. * An append-only file must be opened in append mode for writing.
  1447. */
  1448. if (IS_APPEND(inode)) {
  1449. if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
  1450. return -EPERM;
  1451. if (flag & O_TRUNC)
  1452. return -EPERM;
  1453. }
  1454. /* O_NOATIME can only be set by the owner or superuser */
  1455. if (flag & O_NOATIME)
  1456. if (!is_owner_or_cap(inode))
  1457. return -EPERM;
  1458. /*
  1459. * Ensure there are no outstanding leases on the file.
  1460. */
  1461. error = break_lease(inode, flag);
  1462. if (error)
  1463. return error;
  1464. if (flag & O_TRUNC) {
  1465. error = get_write_access(inode);
  1466. if (error)
  1467. return error;
  1468. /*
  1469. * Refuse to truncate files with mandatory locks held on them.
  1470. */
  1471. error = locks_verify_locked(inode);
  1472. if (!error) {
  1473. DQUOT_INIT(inode);
  1474. error = do_truncate(dentry, 0,
  1475. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1476. NULL);
  1477. }
  1478. put_write_access(inode);
  1479. if (error)
  1480. return error;
  1481. } else
  1482. if (flag & FMODE_WRITE)
  1483. DQUOT_INIT(inode);
  1484. return 0;
  1485. }
  1486. static int open_namei_create(struct nameidata *nd, struct path *path,
  1487. int flag, int mode)
  1488. {
  1489. int error;
  1490. struct dentry *dir = nd->dentry;
  1491. if (!IS_POSIXACL(dir->d_inode))
  1492. mode &= ~current->fs->umask;
  1493. error = vfs_create(dir->d_inode, path->dentry, mode, nd);
  1494. mutex_unlock(&dir->d_inode->i_mutex);
  1495. dput(nd->dentry);
  1496. nd->dentry = path->dentry;
  1497. if (error)
  1498. return error;
  1499. /* Don't check for write permission, don't truncate */
  1500. return may_open(nd, 0, flag & ~O_TRUNC);
  1501. }
  1502. /*
  1503. * open_namei()
  1504. *
  1505. * namei for open - this is in fact almost the whole open-routine.
  1506. *
  1507. * Note that the low bits of "flag" aren't the same as in the open
  1508. * system call - they are 00 - no permissions needed
  1509. * 01 - read permission needed
  1510. * 10 - write permission needed
  1511. * 11 - read/write permissions needed
  1512. * which is a lot more logical, and also allows the "no perm" needed
  1513. * for symlinks (where the permissions are checked later).
  1514. * SMP-safe
  1515. */
  1516. int open_namei(int dfd, const char *pathname, int flag,
  1517. int mode, struct nameidata *nd)
  1518. {
  1519. int acc_mode, error;
  1520. struct path path;
  1521. struct dentry *dir;
  1522. int count = 0;
  1523. acc_mode = ACC_MODE(flag);
  1524. /* O_TRUNC implies we need access checks for write permissions */
  1525. if (flag & O_TRUNC)
  1526. acc_mode |= MAY_WRITE;
  1527. /* Allow the LSM permission hook to distinguish append
  1528. access from general write access. */
  1529. if (flag & O_APPEND)
  1530. acc_mode |= MAY_APPEND;
  1531. /*
  1532. * The simplest case - just a plain lookup.
  1533. */
  1534. if (!(flag & O_CREAT)) {
  1535. error = path_lookup_open(dfd, pathname, lookup_flags(flag),
  1536. nd, flag);
  1537. if (error)
  1538. return error;
  1539. goto ok;
  1540. }
  1541. /*
  1542. * Create - we need to know the parent.
  1543. */
  1544. error = path_lookup_create(dfd,pathname,LOOKUP_PARENT,nd,flag,mode);
  1545. if (error)
  1546. return error;
  1547. /*
  1548. * We have the parent and last component. First of all, check
  1549. * that we are not asked to creat(2) an obvious directory - that
  1550. * will not do.
  1551. */
  1552. error = -EISDIR;
  1553. if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])
  1554. goto exit;
  1555. dir = nd->dentry;
  1556. nd->flags &= ~LOOKUP_PARENT;
  1557. mutex_lock(&dir->d_inode->i_mutex);
  1558. path.dentry = lookup_hash(nd);
  1559. path.mnt = nd->mnt;
  1560. do_last:
  1561. error = PTR_ERR(path.dentry);
  1562. if (IS_ERR(path.dentry)) {
  1563. mutex_unlock(&dir->d_inode->i_mutex);
  1564. goto exit;
  1565. }
  1566. if (IS_ERR(nd->intent.open.file)) {
  1567. mutex_unlock(&dir->d_inode->i_mutex);
  1568. error = PTR_ERR(nd->intent.open.file);
  1569. goto exit_dput;
  1570. }
  1571. /* Negative dentry, just create the file */
  1572. if (!path.dentry->d_inode) {
  1573. error = open_namei_create(nd, &path, flag, mode);
  1574. if (error)
  1575. goto exit;
  1576. return 0;
  1577. }
  1578. /*
  1579. * It already exists.
  1580. */
  1581. mutex_unlock(&dir->d_inode->i_mutex);
  1582. audit_inode(pathname, path.dentry);
  1583. error = -EEXIST;
  1584. if (flag & O_EXCL)
  1585. goto exit_dput;
  1586. if (__follow_mount(&path)) {
  1587. error = -ELOOP;
  1588. if (flag & O_NOFOLLOW)
  1589. goto exit_dput;
  1590. }
  1591. error = -ENOENT;
  1592. if (!path.dentry->d_inode)
  1593. goto exit_dput;
  1594. if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
  1595. goto do_link;
  1596. path_to_nameidata(&path, nd);
  1597. error = -EISDIR;
  1598. if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
  1599. goto exit;
  1600. ok:
  1601. error = may_open(nd, acc_mode, flag);
  1602. if (error)
  1603. goto exit;
  1604. return 0;
  1605. exit_dput:
  1606. dput_path(&path, nd);
  1607. exit:
  1608. if (!IS_ERR(nd->intent.open.file))
  1609. release_open_intent(nd);
  1610. path_release(nd);
  1611. return error;
  1612. do_link:
  1613. error = -ELOOP;
  1614. if (flag & O_NOFOLLOW)
  1615. goto exit_dput;
  1616. /*
  1617. * This is subtle. Instead of calling do_follow_link() we do the
  1618. * thing by hands. The reason is that this way we have zero link_count
  1619. * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
  1620. * After that we have the parent and last component, i.e.
  1621. * we are in the same situation as after the first path_walk().
  1622. * Well, almost - if the last component is normal we get its copy
  1623. * stored in nd->last.name and we will have to putname() it when we
  1624. * are done. Procfs-like symlinks just set LAST_BIND.
  1625. */
  1626. nd->flags |= LOOKUP_PARENT;
  1627. error = security_inode_follow_link(path.dentry, nd);
  1628. if (error)
  1629. goto exit_dput;
  1630. error = __do_follow_link(&path, nd);
  1631. if (error) {
  1632. /* Does someone understand code flow here? Or it is only
  1633. * me so stupid? Anathema to whoever designed this non-sense
  1634. * with "intent.open".
  1635. */
  1636. release_open_intent(nd);
  1637. return error;
  1638. }
  1639. nd->flags &= ~LOOKUP_PARENT;
  1640. if (nd->last_type == LAST_BIND)
  1641. goto ok;
  1642. error = -EISDIR;
  1643. if (nd->last_type != LAST_NORM)
  1644. goto exit;
  1645. if (nd->last.name[nd->last.len]) {
  1646. __putname(nd->last.name);
  1647. goto exit;
  1648. }
  1649. error = -ELOOP;
  1650. if (count++==32) {
  1651. __putname(nd->last.name);
  1652. goto exit;
  1653. }
  1654. dir = nd->dentry;
  1655. mutex_lock(&dir->d_inode->i_mutex);
  1656. path.dentry = lookup_hash(nd);
  1657. path.mnt = nd->mnt;
  1658. __putname(nd->last.name);
  1659. goto do_last;
  1660. }
  1661. /**
  1662. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1663. * @nd: nameidata info
  1664. * @is_dir: directory flag
  1665. *
  1666. * Simple function to lookup and return a dentry and create it
  1667. * if it doesn't exist. Is SMP-safe.
  1668. *
  1669. * Returns with nd->dentry->d_inode->i_mutex locked.
  1670. */
  1671. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1672. {
  1673. struct dentry *dentry = ERR_PTR(-EEXIST);
  1674. mutex_lock_nested(&nd->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1675. /*
  1676. * Yucky last component or no last component at all?
  1677. * (foo/., foo/.., /////)
  1678. */
  1679. if (nd->last_type != LAST_NORM)
  1680. goto fail;
  1681. nd->flags &= ~LOOKUP_PARENT;
  1682. nd->flags |= LOOKUP_CREATE;
  1683. nd->intent.open.flags = O_EXCL;
  1684. /*
  1685. * Do the final lookup.
  1686. */
  1687. dentry = lookup_hash(nd);
  1688. if (IS_ERR(dentry))
  1689. goto fail;
  1690. /*
  1691. * Special case - lookup gave negative, but... we had foo/bar/
  1692. * From the vfs_mknod() POV we just have a negative dentry -
  1693. * all is fine. Let's be bastards - you had / on the end, you've
  1694. * been asking for (non-existent) directory. -ENOENT for you.
  1695. */
  1696. if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode)
  1697. goto enoent;
  1698. return dentry;
  1699. enoent:
  1700. dput(dentry);
  1701. dentry = ERR_PTR(-ENOENT);
  1702. fail:
  1703. return dentry;
  1704. }
  1705. EXPORT_SYMBOL_GPL(lookup_create);
  1706. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1707. {
  1708. int error = may_create(dir, dentry, NULL);
  1709. if (error)
  1710. return error;
  1711. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1712. return -EPERM;
  1713. if (!dir->i_op || !dir->i_op->mknod)
  1714. return -EPERM;
  1715. error = security_inode_mknod(dir, dentry, mode, dev);
  1716. if (error)
  1717. return error;
  1718. DQUOT_INIT(dir);
  1719. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1720. if (!error)
  1721. fsnotify_create(dir, dentry);
  1722. return error;
  1723. }
  1724. asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
  1725. unsigned dev)
  1726. {
  1727. int error = 0;
  1728. char * tmp;
  1729. struct dentry * dentry;
  1730. struct nameidata nd;
  1731. if (S_ISDIR(mode))
  1732. return -EPERM;
  1733. tmp = getname(filename);
  1734. if (IS_ERR(tmp))
  1735. return PTR_ERR(tmp);
  1736. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1737. if (error)
  1738. goto out;
  1739. dentry = lookup_create(&nd, 0);
  1740. error = PTR_ERR(dentry);
  1741. if (!IS_POSIXACL(nd.dentry->d_inode))
  1742. mode &= ~current->fs->umask;
  1743. if (!IS_ERR(dentry)) {
  1744. switch (mode & S_IFMT) {
  1745. case 0: case S_IFREG:
  1746. error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd);
  1747. break;
  1748. case S_IFCHR: case S_IFBLK:
  1749. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,
  1750. new_decode_dev(dev));
  1751. break;
  1752. case S_IFIFO: case S_IFSOCK:
  1753. error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0);
  1754. break;
  1755. case S_IFDIR:
  1756. error = -EPERM;
  1757. break;
  1758. default:
  1759. error = -EINVAL;
  1760. }
  1761. dput(dentry);
  1762. }
  1763. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1764. path_release(&nd);
  1765. out:
  1766. putname(tmp);
  1767. return error;
  1768. }
  1769. asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
  1770. {
  1771. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  1772. }
  1773. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1774. {
  1775. int error = may_create(dir, dentry, NULL);
  1776. if (error)
  1777. return error;
  1778. if (!dir->i_op || !dir->i_op->mkdir)
  1779. return -EPERM;
  1780. mode &= (S_IRWXUGO|S_ISVTX);
  1781. error = security_inode_mkdir(dir, dentry, mode);
  1782. if (error)
  1783. return error;
  1784. DQUOT_INIT(dir);
  1785. error = dir->i_op->mkdir(dir, dentry, mode);
  1786. if (!error)
  1787. fsnotify_mkdir(dir, dentry);
  1788. return error;
  1789. }
  1790. asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
  1791. {
  1792. int error = 0;
  1793. char * tmp;
  1794. struct dentry *dentry;
  1795. struct nameidata nd;
  1796. tmp = getname(pathname);
  1797. error = PTR_ERR(tmp);
  1798. if (IS_ERR(tmp))
  1799. goto out_err;
  1800. error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
  1801. if (error)
  1802. goto out;
  1803. dentry = lookup_create(&nd, 1);
  1804. error = PTR_ERR(dentry);
  1805. if (IS_ERR(dentry))
  1806. goto out_unlock;
  1807. if (!IS_POSIXACL(nd.dentry->d_inode))
  1808. mode &= ~current->fs->umask;
  1809. error = vfs_mkdir(nd.dentry->d_inode, dentry, mode);
  1810. dput(dentry);
  1811. out_unlock:
  1812. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1813. path_release(&nd);
  1814. out:
  1815. putname(tmp);
  1816. out_err:
  1817. return error;
  1818. }
  1819. asmlinkage long sys_mkdir(const char __user *pathname, int mode)
  1820. {
  1821. return sys_mkdirat(AT_FDCWD, pathname, mode);
  1822. }
  1823. /*
  1824. * We try to drop the dentry early: we should have
  1825. * a usage count of 2 if we're the only user of this
  1826. * dentry, and if that is true (possibly after pruning
  1827. * the dcache), then we drop the dentry now.
  1828. *
  1829. * A low-level filesystem can, if it choses, legally
  1830. * do a
  1831. *
  1832. * if (!d_unhashed(dentry))
  1833. * return -EBUSY;
  1834. *
  1835. * if it cannot handle the case of removing a directory
  1836. * that is still in use by something else..
  1837. */
  1838. void dentry_unhash(struct dentry *dentry)
  1839. {
  1840. dget(dentry);
  1841. shrink_dcache_parent(dentry);
  1842. spin_lock(&dcache_lock);
  1843. spin_lock(&dentry->d_lock);
  1844. if (atomic_read(&dentry->d_count) == 2)
  1845. __d_drop(dentry);
  1846. spin_unlock(&dentry->d_lock);
  1847. spin_unlock(&dcache_lock);
  1848. }
  1849. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1850. {
  1851. int error = may_delete(dir, dentry, 1);
  1852. if (error)
  1853. return error;
  1854. if (!dir->i_op || !dir->i_op->rmdir)
  1855. return -EPERM;
  1856. DQUOT_INIT(dir);
  1857. mutex_lock(&dentry->d_inode->i_mutex);
  1858. dentry_unhash(dentry);
  1859. if (d_mountpoint(dentry))
  1860. error = -EBUSY;
  1861. else {
  1862. error = security_inode_rmdir(dir, dentry);
  1863. if (!error) {
  1864. error = dir->i_op->rmdir(dir, dentry);
  1865. if (!error)
  1866. dentry->d_inode->i_flags |= S_DEAD;
  1867. }
  1868. }
  1869. mutex_unlock(&dentry->d_inode->i_mutex);
  1870. if (!error) {
  1871. d_delete(dentry);
  1872. }
  1873. dput(dentry);
  1874. return error;
  1875. }
  1876. static long do_rmdir(int dfd, const char __user *pathname)
  1877. {
  1878. int error = 0;
  1879. char * name;
  1880. struct dentry *dentry;
  1881. struct nameidata nd;
  1882. name = getname(pathname);
  1883. if(IS_ERR(name))
  1884. return PTR_ERR(name);
  1885. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  1886. if (error)
  1887. goto exit;
  1888. switch(nd.last_type) {
  1889. case LAST_DOTDOT:
  1890. error = -ENOTEMPTY;
  1891. goto exit1;
  1892. case LAST_DOT:
  1893. error = -EINVAL;
  1894. goto exit1;
  1895. case LAST_ROOT:
  1896. error = -EBUSY;
  1897. goto exit1;
  1898. }
  1899. mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1900. dentry = lookup_hash(&nd);
  1901. error = PTR_ERR(dentry);
  1902. if (IS_ERR(dentry))
  1903. goto exit2;
  1904. error = vfs_rmdir(nd.dentry->d_inode, dentry);
  1905. dput(dentry);
  1906. exit2:
  1907. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1908. exit1:
  1909. path_release(&nd);
  1910. exit:
  1911. putname(name);
  1912. return error;
  1913. }
  1914. asmlinkage long sys_rmdir(const char __user *pathname)
  1915. {
  1916. return do_rmdir(AT_FDCWD, pathname);
  1917. }
  1918. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1919. {
  1920. int error = may_delete(dir, dentry, 0);
  1921. if (error)
  1922. return error;
  1923. if (!dir->i_op || !dir->i_op->unlink)
  1924. return -EPERM;
  1925. DQUOT_INIT(dir);
  1926. mutex_lock(&dentry->d_inode->i_mutex);
  1927. if (d_mountpoint(dentry))
  1928. error = -EBUSY;
  1929. else {
  1930. error = security_inode_unlink(dir, dentry);
  1931. if (!error)
  1932. error = dir->i_op->unlink(dir, dentry);
  1933. }
  1934. mutex_unlock(&dentry->d_inode->i_mutex);
  1935. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  1936. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  1937. fsnotify_link_count(dentry->d_inode);
  1938. d_delete(dentry);
  1939. }
  1940. return error;
  1941. }
  1942. /*
  1943. * Make sure that the actual truncation of the file will occur outside its
  1944. * directory's i_mutex. Truncate can take a long time if there is a lot of
  1945. * writeout happening, and we don't want to prevent access to the directory
  1946. * while waiting on the I/O.
  1947. */
  1948. static long do_unlinkat(int dfd, const char __user *pathname)
  1949. {
  1950. int error = 0;
  1951. char * name;
  1952. struct dentry *dentry;
  1953. struct nameidata nd;
  1954. struct inode *inode = NULL;
  1955. name = getname(pathname);
  1956. if(IS_ERR(name))
  1957. return PTR_ERR(name);
  1958. error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
  1959. if (error)
  1960. goto exit;
  1961. error = -EISDIR;
  1962. if (nd.last_type != LAST_NORM)
  1963. goto exit1;
  1964. mutex_lock_nested(&nd.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1965. dentry = lookup_hash(&nd);
  1966. error = PTR_ERR(dentry);
  1967. if (!IS_ERR(dentry)) {
  1968. /* Why not before? Because we want correct error value */
  1969. if (nd.last.name[nd.last.len])
  1970. goto slashes;
  1971. inode = dentry->d_inode;
  1972. if (inode)
  1973. atomic_inc(&inode->i_count);
  1974. error = vfs_unlink(nd.dentry->d_inode, dentry);
  1975. exit2:
  1976. dput(dentry);
  1977. }
  1978. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  1979. if (inode)
  1980. iput(inode); /* truncate the inode here */
  1981. exit1:
  1982. path_release(&nd);
  1983. exit:
  1984. putname(name);
  1985. return error;
  1986. slashes:
  1987. error = !dentry->d_inode ? -ENOENT :
  1988. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  1989. goto exit2;
  1990. }
  1991. asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
  1992. {
  1993. if ((flag & ~AT_REMOVEDIR) != 0)
  1994. return -EINVAL;
  1995. if (flag & AT_REMOVEDIR)
  1996. return do_rmdir(dfd, pathname);
  1997. return do_unlinkat(dfd, pathname);
  1998. }
  1999. asmlinkage long sys_unlink(const char __user *pathname)
  2000. {
  2001. return do_unlinkat(AT_FDCWD, pathname);
  2002. }
  2003. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode)
  2004. {
  2005. int error = may_create(dir, dentry, NULL);
  2006. if (error)
  2007. return error;
  2008. if (!dir->i_op || !dir->i_op->symlink)
  2009. return -EPERM;
  2010. error = security_inode_symlink(dir, dentry, oldname);
  2011. if (error)
  2012. return error;
  2013. DQUOT_INIT(dir);
  2014. error = dir->i_op->symlink(dir, dentry, oldname);
  2015. if (!error)
  2016. fsnotify_create(dir, dentry);
  2017. return error;
  2018. }
  2019. asmlinkage long sys_symlinkat(const char __user *oldname,
  2020. int newdfd, const char __user *newname)
  2021. {
  2022. int error = 0;
  2023. char * from;
  2024. char * to;
  2025. struct dentry *dentry;
  2026. struct nameidata nd;
  2027. from = getname(oldname);
  2028. if(IS_ERR(from))
  2029. return PTR_ERR(from);
  2030. to = getname(newname);
  2031. error = PTR_ERR(to);
  2032. if (IS_ERR(to))
  2033. goto out_putname;
  2034. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  2035. if (error)
  2036. goto out;
  2037. dentry = lookup_create(&nd, 0);
  2038. error = PTR_ERR(dentry);
  2039. if (IS_ERR(dentry))
  2040. goto out_unlock;
  2041. error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO);
  2042. dput(dentry);
  2043. out_unlock:
  2044. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  2045. path_release(&nd);
  2046. out:
  2047. putname(to);
  2048. out_putname:
  2049. putname(from);
  2050. return error;
  2051. }
  2052. asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
  2053. {
  2054. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2055. }
  2056. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2057. {
  2058. struct inode *inode = old_dentry->d_inode;
  2059. int error;
  2060. if (!inode)
  2061. return -ENOENT;
  2062. error = may_create(dir, new_dentry, NULL);
  2063. if (error)
  2064. return error;
  2065. if (dir->i_sb != inode->i_sb)
  2066. return -EXDEV;
  2067. /*
  2068. * A link to an append-only or immutable file cannot be created.
  2069. */
  2070. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2071. return -EPERM;
  2072. if (!dir->i_op || !dir->i_op->link)
  2073. return -EPERM;
  2074. if (S_ISDIR(old_dentry->d_inode->i_mode))
  2075. return -EPERM;
  2076. error = security_inode_link(old_dentry, dir, new_dentry);
  2077. if (error)
  2078. return error;
  2079. mutex_lock(&old_dentry->d_inode->i_mutex);
  2080. DQUOT_INIT(dir);
  2081. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2082. mutex_unlock(&old_dentry->d_inode->i_mutex);
  2083. if (!error)
  2084. fsnotify_link(dir, old_dentry->d_inode, new_dentry);
  2085. return error;
  2086. }
  2087. /*
  2088. * Hardlinks are often used in delicate situations. We avoid
  2089. * security-related surprises by not following symlinks on the
  2090. * newname. --KAB
  2091. *
  2092. * We don't follow them on the oldname either to be compatible
  2093. * with linux 2.0, and to avoid hard-linking to directories
  2094. * and other special files. --ADM
  2095. */
  2096. asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
  2097. int newdfd, const char __user *newname,
  2098. int flags)
  2099. {
  2100. struct dentry *new_dentry;
  2101. struct nameidata nd, old_nd;
  2102. int error;
  2103. char * to;
  2104. if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
  2105. return -EINVAL;
  2106. to = getname(newname);
  2107. if (IS_ERR(to))
  2108. return PTR_ERR(to);
  2109. error = __user_walk_fd(olddfd, oldname,
  2110. flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
  2111. &old_nd);
  2112. if (error)
  2113. goto exit;
  2114. error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
  2115. if (error)
  2116. goto out;
  2117. error = -EXDEV;
  2118. if (old_nd.mnt != nd.mnt)
  2119. goto out_release;
  2120. new_dentry = lookup_create(&nd, 0);
  2121. error = PTR_ERR(new_dentry);
  2122. if (IS_ERR(new_dentry))
  2123. goto out_unlock;
  2124. error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry);
  2125. dput(new_dentry);
  2126. out_unlock:
  2127. mutex_unlock(&nd.dentry->d_inode->i_mutex);
  2128. out_release:
  2129. path_release(&nd);
  2130. out:
  2131. path_release(&old_nd);
  2132. exit:
  2133. putname(to);
  2134. return error;
  2135. }
  2136. asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
  2137. {
  2138. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2139. }
  2140. /*
  2141. * The worst of all namespace operations - renaming directory. "Perverted"
  2142. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2143. * Problems:
  2144. * a) we can get into loop creation. Check is done in is_subdir().
  2145. * b) race potential - two innocent renames can create a loop together.
  2146. * That's where 4.4 screws up. Current fix: serialization on
  2147. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2148. * story.
  2149. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2150. * And that - after we got ->i_mutex on parents (until then we don't know
  2151. * whether the target exists). Solution: try to be smart with locking
  2152. * order for inodes. We rely on the fact that tree topology may change
  2153. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2154. * move will be locked. Thus we can rank directories by the tree
  2155. * (ancestors first) and rank all non-directories after them.
  2156. * That works since everybody except rename does "lock parent, lookup,
  2157. * lock child" and rename is under ->s_vfs_rename_mutex.
  2158. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2159. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2160. * we'd better make sure that there's no link(2) for them.
  2161. * d) some filesystems don't support opened-but-unlinked directories,
  2162. * either because of layout or because they are not ready to deal with
  2163. * all cases correctly. The latter will be fixed (taking this sort of
  2164. * stuff into VFS), but the former is not going away. Solution: the same
  2165. * trick as in rmdir().
  2166. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2167. * we are removing the target. Solution: we will have to grab ->i_mutex
  2168. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2169. * ->i_mutex on parents, which works but leads to some truely excessive
  2170. * locking].
  2171. */
  2172. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2173. struct inode *new_dir, struct dentry *new_dentry)
  2174. {
  2175. int error = 0;
  2176. struct inode *target;
  2177. /*
  2178. * If we are going to change the parent - check write permissions,
  2179. * we'll need to flip '..'.
  2180. */
  2181. if (new_dir != old_dir) {
  2182. error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
  2183. if (error)
  2184. return error;
  2185. }
  2186. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2187. if (error)
  2188. return error;
  2189. target = new_dentry->d_inode;
  2190. if (target) {
  2191. mutex_lock(&target->i_mutex);
  2192. dentry_unhash(new_dentry);
  2193. }
  2194. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2195. error = -EBUSY;
  2196. else
  2197. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2198. if (target) {
  2199. if (!error)
  2200. target->i_flags |= S_DEAD;
  2201. mutex_unlock(&target->i_mutex);
  2202. if (d_unhashed(new_dentry))
  2203. d_rehash(new_dentry);
  2204. dput(new_dentry);
  2205. }
  2206. if (!error)
  2207. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2208. d_move(old_dentry,new_dentry);
  2209. return error;
  2210. }
  2211. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2212. struct inode *new_dir, struct dentry *new_dentry)
  2213. {
  2214. struct inode *target;
  2215. int error;
  2216. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2217. if (error)
  2218. return error;
  2219. dget(new_dentry);
  2220. target = new_dentry->d_inode;
  2221. if (target)
  2222. mutex_lock(&target->i_mutex);
  2223. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2224. error = -EBUSY;
  2225. else
  2226. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2227. if (!error) {
  2228. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2229. d_move(old_dentry, new_dentry);
  2230. }
  2231. if (target)
  2232. mutex_unlock(&target->i_mutex);
  2233. dput(new_dentry);
  2234. return error;
  2235. }
  2236. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2237. struct inode *new_dir, struct dentry *new_dentry)
  2238. {
  2239. int error;
  2240. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2241. const char *old_name;
  2242. if (old_dentry->d_inode == new_dentry->d_inode)
  2243. return 0;
  2244. error = may_delete(old_dir, old_dentry, is_dir);
  2245. if (error)
  2246. return error;
  2247. if (!new_dentry->d_inode)
  2248. error = may_create(new_dir, new_dentry, NULL);
  2249. else
  2250. error = may_delete(new_dir, new_dentry, is_dir);
  2251. if (error)
  2252. return error;
  2253. if (!old_dir->i_op || !old_dir->i_op->rename)
  2254. return -EPERM;
  2255. DQUOT_INIT(old_dir);
  2256. DQUOT_INIT(new_dir);
  2257. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2258. if (is_dir)
  2259. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2260. else
  2261. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2262. if (!error) {
  2263. const char *new_name = old_dentry->d_name.name;
  2264. fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
  2265. new_dentry->d_inode, old_dentry);
  2266. }
  2267. fsnotify_oldname_free(old_name);
  2268. return error;
  2269. }
  2270. static int do_rename(int olddfd, const char *oldname,
  2271. int newdfd, const char *newname)
  2272. {
  2273. int error = 0;
  2274. struct dentry * old_dir, * new_dir;
  2275. struct dentry * old_dentry, *new_dentry;
  2276. struct dentry * trap;
  2277. struct nameidata oldnd, newnd;
  2278. error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
  2279. if (error)
  2280. goto exit;
  2281. error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
  2282. if (error)
  2283. goto exit1;
  2284. error = -EXDEV;
  2285. if (oldnd.mnt != newnd.mnt)
  2286. goto exit2;
  2287. old_dir = oldnd.dentry;
  2288. error = -EBUSY;
  2289. if (oldnd.last_type != LAST_NORM)
  2290. goto exit2;
  2291. new_dir = newnd.dentry;
  2292. if (newnd.last_type != LAST_NORM)
  2293. goto exit2;
  2294. trap = lock_rename(new_dir, old_dir);
  2295. old_dentry = lookup_hash(&oldnd);
  2296. error = PTR_ERR(old_dentry);
  2297. if (IS_ERR(old_dentry))
  2298. goto exit3;
  2299. /* source must exist */
  2300. error = -ENOENT;
  2301. if (!old_dentry->d_inode)
  2302. goto exit4;
  2303. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2304. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2305. error = -ENOTDIR;
  2306. if (oldnd.last.name[oldnd.last.len])
  2307. goto exit4;
  2308. if (newnd.last.name[newnd.last.len])
  2309. goto exit4;
  2310. }
  2311. /* source should not be ancestor of target */
  2312. error = -EINVAL;
  2313. if (old_dentry == trap)
  2314. goto exit4;
  2315. new_dentry = lookup_hash(&newnd);
  2316. error = PTR_ERR(new_dentry);
  2317. if (IS_ERR(new_dentry))
  2318. goto exit4;
  2319. /* target should not be an ancestor of source */
  2320. error = -ENOTEMPTY;
  2321. if (new_dentry == trap)
  2322. goto exit5;
  2323. error = vfs_rename(old_dir->d_inode, old_dentry,
  2324. new_dir->d_inode, new_dentry);
  2325. exit5:
  2326. dput(new_dentry);
  2327. exit4:
  2328. dput(old_dentry);
  2329. exit3:
  2330. unlock_rename(new_dir, old_dir);
  2331. exit2:
  2332. path_release(&newnd);
  2333. exit1:
  2334. path_release(&oldnd);
  2335. exit:
  2336. return error;
  2337. }
  2338. asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
  2339. int newdfd, const char __user *newname)
  2340. {
  2341. int error;
  2342. char * from;
  2343. char * to;
  2344. from = getname(oldname);
  2345. if(IS_ERR(from))
  2346. return PTR_ERR(from);
  2347. to = getname(newname);
  2348. error = PTR_ERR(to);
  2349. if (!IS_ERR(to)) {
  2350. error = do_rename(olddfd, from, newdfd, to);
  2351. putname(to);
  2352. }
  2353. putname(from);
  2354. return error;
  2355. }
  2356. asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
  2357. {
  2358. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2359. }
  2360. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2361. {
  2362. int len;
  2363. len = PTR_ERR(link);
  2364. if (IS_ERR(link))
  2365. goto out;
  2366. len = strlen(link);
  2367. if (len > (unsigned) buflen)
  2368. len = buflen;
  2369. if (copy_to_user(buffer, link, len))
  2370. len = -EFAULT;
  2371. out:
  2372. return len;
  2373. }
  2374. /*
  2375. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2376. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2377. * using) it for any given inode is up to filesystem.
  2378. */
  2379. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2380. {
  2381. struct nameidata nd;
  2382. void *cookie;
  2383. nd.depth = 0;
  2384. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2385. if (!IS_ERR(cookie)) {
  2386. int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2387. if (dentry->d_inode->i_op->put_link)
  2388. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2389. cookie = ERR_PTR(res);
  2390. }
  2391. return PTR_ERR(cookie);
  2392. }
  2393. int vfs_follow_link(struct nameidata *nd, const char *link)
  2394. {
  2395. return __vfs_follow_link(nd, link);
  2396. }
  2397. /* get the link contents into pagecache */
  2398. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2399. {
  2400. struct page * page;
  2401. struct address_space *mapping = dentry->d_inode->i_mapping;
  2402. page = read_mapping_page(mapping, 0, NULL);
  2403. if (IS_ERR(page))
  2404. return (char*)page;
  2405. *ppage = page;
  2406. return kmap(page);
  2407. }
  2408. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2409. {
  2410. struct page *page = NULL;
  2411. char *s = page_getlink(dentry, &page);
  2412. int res = vfs_readlink(dentry,buffer,buflen,s);
  2413. if (page) {
  2414. kunmap(page);
  2415. page_cache_release(page);
  2416. }
  2417. return res;
  2418. }
  2419. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2420. {
  2421. struct page *page = NULL;
  2422. nd_set_link(nd, page_getlink(dentry, &page));
  2423. return page;
  2424. }
  2425. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2426. {
  2427. struct page *page = cookie;
  2428. if (page) {
  2429. kunmap(page);
  2430. page_cache_release(page);
  2431. }
  2432. }
  2433. int __page_symlink(struct inode *inode, const char *symname, int len,
  2434. gfp_t gfp_mask)
  2435. {
  2436. struct address_space *mapping = inode->i_mapping;
  2437. struct page *page;
  2438. void *fsdata;
  2439. int err;
  2440. char *kaddr;
  2441. retry:
  2442. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2443. AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
  2444. if (err)
  2445. goto fail;
  2446. kaddr = kmap_atomic(page, KM_USER0);
  2447. memcpy(kaddr, symname, len-1);
  2448. kunmap_atomic(kaddr, KM_USER0);
  2449. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2450. page, fsdata);
  2451. if (err < 0)
  2452. goto fail;
  2453. if (err < len-1)
  2454. goto retry;
  2455. mark_inode_dirty(inode);
  2456. return 0;
  2457. fail:
  2458. return err;
  2459. }
  2460. int page_symlink(struct inode *inode, const char *symname, int len)
  2461. {
  2462. return __page_symlink(inode, symname, len,
  2463. mapping_gfp_mask(inode->i_mapping));
  2464. }
  2465. const struct inode_operations page_symlink_inode_operations = {
  2466. .readlink = generic_readlink,
  2467. .follow_link = page_follow_link_light,
  2468. .put_link = page_put_link,
  2469. };
  2470. EXPORT_SYMBOL(__user_walk);
  2471. EXPORT_SYMBOL(__user_walk_fd);
  2472. EXPORT_SYMBOL(follow_down);
  2473. EXPORT_SYMBOL(follow_up);
  2474. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2475. EXPORT_SYMBOL(getname);
  2476. EXPORT_SYMBOL(lock_rename);
  2477. EXPORT_SYMBOL(lookup_one_len);
  2478. EXPORT_SYMBOL(page_follow_link_light);
  2479. EXPORT_SYMBOL(page_put_link);
  2480. EXPORT_SYMBOL(page_readlink);
  2481. EXPORT_SYMBOL(__page_symlink);
  2482. EXPORT_SYMBOL(page_symlink);
  2483. EXPORT_SYMBOL(page_symlink_inode_operations);
  2484. EXPORT_SYMBOL(path_lookup);
  2485. EXPORT_SYMBOL(vfs_path_lookup);
  2486. EXPORT_SYMBOL(path_release);
  2487. EXPORT_SYMBOL(permission);
  2488. EXPORT_SYMBOL(vfs_permission);
  2489. EXPORT_SYMBOL(file_permission);
  2490. EXPORT_SYMBOL(unlock_rename);
  2491. EXPORT_SYMBOL(vfs_create);
  2492. EXPORT_SYMBOL(vfs_follow_link);
  2493. EXPORT_SYMBOL(vfs_link);
  2494. EXPORT_SYMBOL(vfs_mkdir);
  2495. EXPORT_SYMBOL(vfs_mknod);
  2496. EXPORT_SYMBOL(generic_permission);
  2497. EXPORT_SYMBOL(vfs_readlink);
  2498. EXPORT_SYMBOL(vfs_rename);
  2499. EXPORT_SYMBOL(vfs_rmdir);
  2500. EXPORT_SYMBOL(vfs_symlink);
  2501. EXPORT_SYMBOL(vfs_unlink);
  2502. EXPORT_SYMBOL(dentry_unhash);
  2503. EXPORT_SYMBOL(generic_readlink);