namei.c 66 KB

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