namei.c 68 KB

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