namei.c 68 KB

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