namei.c 67 KB

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