namespace.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635
  1. /*
  2. * linux/fs/namespace.c
  3. *
  4. * (C) Copyright Al Viro 2000, 2001
  5. * Released under GPL v2.
  6. *
  7. * Based on code from fs/super.c, copyright Linus Torvalds and others.
  8. * Heavily rewritten.
  9. */
  10. #include <linux/syscalls.h>
  11. #include <linux/export.h>
  12. #include <linux/capability.h>
  13. #include <linux/mnt_namespace.h>
  14. #include <linux/namei.h>
  15. #include <linux/security.h>
  16. #include <linux/idr.h>
  17. #include <linux/acct.h> /* acct_auto_close_mnt */
  18. #include <linux/ramfs.h> /* init_rootfs */
  19. #include <linux/fs_struct.h> /* get_fs_root et.al. */
  20. #include <linux/fsnotify.h> /* fsnotify_vfsmount_delete */
  21. #include <linux/uaccess.h>
  22. #include "pnode.h"
  23. #include "internal.h"
  24. #define HASH_SHIFT ilog2(PAGE_SIZE / sizeof(struct list_head))
  25. #define HASH_SIZE (1UL << HASH_SHIFT)
  26. static int event;
  27. static DEFINE_IDA(mnt_id_ida);
  28. static DEFINE_IDA(mnt_group_ida);
  29. static DEFINE_SPINLOCK(mnt_id_lock);
  30. static int mnt_id_start = 0;
  31. static int mnt_group_start = 1;
  32. static struct list_head *mount_hashtable __read_mostly;
  33. static struct kmem_cache *mnt_cache __read_mostly;
  34. static struct rw_semaphore namespace_sem;
  35. /* /sys/fs */
  36. struct kobject *fs_kobj;
  37. EXPORT_SYMBOL_GPL(fs_kobj);
  38. /*
  39. * vfsmount lock may be taken for read to prevent changes to the
  40. * vfsmount hash, ie. during mountpoint lookups or walking back
  41. * up the tree.
  42. *
  43. * It should be taken for write in all cases where the vfsmount
  44. * tree or hash is modified or when a vfsmount structure is modified.
  45. */
  46. DEFINE_BRLOCK(vfsmount_lock);
  47. static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
  48. {
  49. unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
  50. tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
  51. tmp = tmp + (tmp >> HASH_SHIFT);
  52. return tmp & (HASH_SIZE - 1);
  53. }
  54. #define MNT_WRITER_UNDERFLOW_LIMIT -(1<<16)
  55. /*
  56. * allocation is serialized by namespace_sem, but we need the spinlock to
  57. * serialize with freeing.
  58. */
  59. static int mnt_alloc_id(struct mount *mnt)
  60. {
  61. int res;
  62. retry:
  63. ida_pre_get(&mnt_id_ida, GFP_KERNEL);
  64. spin_lock(&mnt_id_lock);
  65. res = ida_get_new_above(&mnt_id_ida, mnt_id_start, &mnt->mnt_id);
  66. if (!res)
  67. mnt_id_start = mnt->mnt_id + 1;
  68. spin_unlock(&mnt_id_lock);
  69. if (res == -EAGAIN)
  70. goto retry;
  71. return res;
  72. }
  73. static void mnt_free_id(struct mount *mnt)
  74. {
  75. int id = mnt->mnt_id;
  76. spin_lock(&mnt_id_lock);
  77. ida_remove(&mnt_id_ida, id);
  78. if (mnt_id_start > id)
  79. mnt_id_start = id;
  80. spin_unlock(&mnt_id_lock);
  81. }
  82. /*
  83. * Allocate a new peer group ID
  84. *
  85. * mnt_group_ida is protected by namespace_sem
  86. */
  87. static int mnt_alloc_group_id(struct mount *mnt)
  88. {
  89. int res;
  90. if (!ida_pre_get(&mnt_group_ida, GFP_KERNEL))
  91. return -ENOMEM;
  92. res = ida_get_new_above(&mnt_group_ida,
  93. mnt_group_start,
  94. &mnt->mnt_group_id);
  95. if (!res)
  96. mnt_group_start = mnt->mnt_group_id + 1;
  97. return res;
  98. }
  99. /*
  100. * Release a peer group ID
  101. */
  102. void mnt_release_group_id(struct mount *mnt)
  103. {
  104. int id = mnt->mnt_group_id;
  105. ida_remove(&mnt_group_ida, id);
  106. if (mnt_group_start > id)
  107. mnt_group_start = id;
  108. mnt->mnt_group_id = 0;
  109. }
  110. /*
  111. * vfsmount lock must be held for read
  112. */
  113. static inline void mnt_add_count(struct mount *mnt, int n)
  114. {
  115. #ifdef CONFIG_SMP
  116. this_cpu_add(mnt->mnt_pcp->mnt_count, n);
  117. #else
  118. preempt_disable();
  119. mnt->mnt_count += n;
  120. preempt_enable();
  121. #endif
  122. }
  123. /*
  124. * vfsmount lock must be held for write
  125. */
  126. unsigned int mnt_get_count(struct mount *mnt)
  127. {
  128. #ifdef CONFIG_SMP
  129. unsigned int count = 0;
  130. int cpu;
  131. for_each_possible_cpu(cpu) {
  132. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_count;
  133. }
  134. return count;
  135. #else
  136. return mnt->mnt_count;
  137. #endif
  138. }
  139. static struct mount *alloc_vfsmnt(const char *name)
  140. {
  141. struct mount *mnt = kmem_cache_zalloc(mnt_cache, GFP_KERNEL);
  142. if (mnt) {
  143. int err;
  144. err = mnt_alloc_id(mnt);
  145. if (err)
  146. goto out_free_cache;
  147. if (name) {
  148. mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
  149. if (!mnt->mnt_devname)
  150. goto out_free_id;
  151. }
  152. #ifdef CONFIG_SMP
  153. mnt->mnt_pcp = alloc_percpu(struct mnt_pcp);
  154. if (!mnt->mnt_pcp)
  155. goto out_free_devname;
  156. this_cpu_add(mnt->mnt_pcp->mnt_count, 1);
  157. #else
  158. mnt->mnt_count = 1;
  159. mnt->mnt_writers = 0;
  160. #endif
  161. INIT_LIST_HEAD(&mnt->mnt_hash);
  162. INIT_LIST_HEAD(&mnt->mnt_child);
  163. INIT_LIST_HEAD(&mnt->mnt_mounts);
  164. INIT_LIST_HEAD(&mnt->mnt_list);
  165. INIT_LIST_HEAD(&mnt->mnt_expire);
  166. INIT_LIST_HEAD(&mnt->mnt_share);
  167. INIT_LIST_HEAD(&mnt->mnt_slave_list);
  168. INIT_LIST_HEAD(&mnt->mnt_slave);
  169. #ifdef CONFIG_FSNOTIFY
  170. INIT_HLIST_HEAD(&mnt->mnt_fsnotify_marks);
  171. #endif
  172. }
  173. return mnt;
  174. #ifdef CONFIG_SMP
  175. out_free_devname:
  176. kfree(mnt->mnt_devname);
  177. #endif
  178. out_free_id:
  179. mnt_free_id(mnt);
  180. out_free_cache:
  181. kmem_cache_free(mnt_cache, mnt);
  182. return NULL;
  183. }
  184. /*
  185. * Most r/o checks on a fs are for operations that take
  186. * discrete amounts of time, like a write() or unlink().
  187. * We must keep track of when those operations start
  188. * (for permission checks) and when they end, so that
  189. * we can determine when writes are able to occur to
  190. * a filesystem.
  191. */
  192. /*
  193. * __mnt_is_readonly: check whether a mount is read-only
  194. * @mnt: the mount to check for its write status
  195. *
  196. * This shouldn't be used directly ouside of the VFS.
  197. * It does not guarantee that the filesystem will stay
  198. * r/w, just that it is right *now*. This can not and
  199. * should not be used in place of IS_RDONLY(inode).
  200. * mnt_want/drop_write() will _keep_ the filesystem
  201. * r/w.
  202. */
  203. int __mnt_is_readonly(struct vfsmount *mnt)
  204. {
  205. if (mnt->mnt_flags & MNT_READONLY)
  206. return 1;
  207. if (mnt->mnt_sb->s_flags & MS_RDONLY)
  208. return 1;
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(__mnt_is_readonly);
  212. static inline void mnt_inc_writers(struct mount *mnt)
  213. {
  214. #ifdef CONFIG_SMP
  215. this_cpu_inc(mnt->mnt_pcp->mnt_writers);
  216. #else
  217. mnt->mnt_writers++;
  218. #endif
  219. }
  220. static inline void mnt_dec_writers(struct mount *mnt)
  221. {
  222. #ifdef CONFIG_SMP
  223. this_cpu_dec(mnt->mnt_pcp->mnt_writers);
  224. #else
  225. mnt->mnt_writers--;
  226. #endif
  227. }
  228. static unsigned int mnt_get_writers(struct mount *mnt)
  229. {
  230. #ifdef CONFIG_SMP
  231. unsigned int count = 0;
  232. int cpu;
  233. for_each_possible_cpu(cpu) {
  234. count += per_cpu_ptr(mnt->mnt_pcp, cpu)->mnt_writers;
  235. }
  236. return count;
  237. #else
  238. return mnt->mnt_writers;
  239. #endif
  240. }
  241. static int mnt_is_readonly(struct vfsmount *mnt)
  242. {
  243. if (mnt->mnt_sb->s_readonly_remount)
  244. return 1;
  245. /* Order wrt setting s_flags/s_readonly_remount in do_remount() */
  246. smp_rmb();
  247. return __mnt_is_readonly(mnt);
  248. }
  249. /*
  250. * Most r/o checks on a fs are for operations that take
  251. * discrete amounts of time, like a write() or unlink().
  252. * We must keep track of when those operations start
  253. * (for permission checks) and when they end, so that
  254. * we can determine when writes are able to occur to
  255. * a filesystem.
  256. */
  257. /**
  258. * mnt_want_write - get write access to a mount
  259. * @m: the mount on which to take a write
  260. *
  261. * This tells the low-level filesystem that a write is
  262. * about to be performed to it, and makes sure that
  263. * writes are allowed before returning success. When
  264. * the write operation is finished, mnt_drop_write()
  265. * must be called. This is effectively a refcount.
  266. */
  267. int mnt_want_write(struct vfsmount *m)
  268. {
  269. struct mount *mnt = real_mount(m);
  270. int ret = 0;
  271. preempt_disable();
  272. mnt_inc_writers(mnt);
  273. /*
  274. * The store to mnt_inc_writers must be visible before we pass
  275. * MNT_WRITE_HOLD loop below, so that the slowpath can see our
  276. * incremented count after it has set MNT_WRITE_HOLD.
  277. */
  278. smp_mb();
  279. while (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
  280. cpu_relax();
  281. /*
  282. * After the slowpath clears MNT_WRITE_HOLD, mnt_is_readonly will
  283. * be set to match its requirements. So we must not load that until
  284. * MNT_WRITE_HOLD is cleared.
  285. */
  286. smp_rmb();
  287. if (mnt_is_readonly(m)) {
  288. mnt_dec_writers(mnt);
  289. ret = -EROFS;
  290. }
  291. preempt_enable();
  292. return ret;
  293. }
  294. EXPORT_SYMBOL_GPL(mnt_want_write);
  295. /**
  296. * mnt_clone_write - get write access to a mount
  297. * @mnt: the mount on which to take a write
  298. *
  299. * This is effectively like mnt_want_write, except
  300. * it must only be used to take an extra write reference
  301. * on a mountpoint that we already know has a write reference
  302. * on it. This allows some optimisation.
  303. *
  304. * After finished, mnt_drop_write must be called as usual to
  305. * drop the reference.
  306. */
  307. int mnt_clone_write(struct vfsmount *mnt)
  308. {
  309. /* superblock may be r/o */
  310. if (__mnt_is_readonly(mnt))
  311. return -EROFS;
  312. preempt_disable();
  313. mnt_inc_writers(real_mount(mnt));
  314. preempt_enable();
  315. return 0;
  316. }
  317. EXPORT_SYMBOL_GPL(mnt_clone_write);
  318. /**
  319. * mnt_want_write_file - get write access to a file's mount
  320. * @file: the file who's mount on which to take a write
  321. *
  322. * This is like mnt_want_write, but it takes a file and can
  323. * do some optimisations if the file is open for write already
  324. */
  325. int mnt_want_write_file(struct file *file)
  326. {
  327. struct inode *inode = file->f_dentry->d_inode;
  328. if (!(file->f_mode & FMODE_WRITE) || special_file(inode->i_mode))
  329. return mnt_want_write(file->f_path.mnt);
  330. else
  331. return mnt_clone_write(file->f_path.mnt);
  332. }
  333. EXPORT_SYMBOL_GPL(mnt_want_write_file);
  334. /**
  335. * mnt_drop_write - give up write access to a mount
  336. * @mnt: the mount on which to give up write access
  337. *
  338. * Tells the low-level filesystem that we are done
  339. * performing writes to it. Must be matched with
  340. * mnt_want_write() call above.
  341. */
  342. void mnt_drop_write(struct vfsmount *mnt)
  343. {
  344. preempt_disable();
  345. mnt_dec_writers(real_mount(mnt));
  346. preempt_enable();
  347. }
  348. EXPORT_SYMBOL_GPL(mnt_drop_write);
  349. void mnt_drop_write_file(struct file *file)
  350. {
  351. mnt_drop_write(file->f_path.mnt);
  352. }
  353. EXPORT_SYMBOL(mnt_drop_write_file);
  354. static int mnt_make_readonly(struct mount *mnt)
  355. {
  356. int ret = 0;
  357. br_write_lock(vfsmount_lock);
  358. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  359. /*
  360. * After storing MNT_WRITE_HOLD, we'll read the counters. This store
  361. * should be visible before we do.
  362. */
  363. smp_mb();
  364. /*
  365. * With writers on hold, if this value is zero, then there are
  366. * definitely no active writers (although held writers may subsequently
  367. * increment the count, they'll have to wait, and decrement it after
  368. * seeing MNT_READONLY).
  369. *
  370. * It is OK to have counter incremented on one CPU and decremented on
  371. * another: the sum will add up correctly. The danger would be when we
  372. * sum up each counter, if we read a counter before it is incremented,
  373. * but then read another CPU's count which it has been subsequently
  374. * decremented from -- we would see more decrements than we should.
  375. * MNT_WRITE_HOLD protects against this scenario, because
  376. * mnt_want_write first increments count, then smp_mb, then spins on
  377. * MNT_WRITE_HOLD, so it can't be decremented by another CPU while
  378. * we're counting up here.
  379. */
  380. if (mnt_get_writers(mnt) > 0)
  381. ret = -EBUSY;
  382. else
  383. mnt->mnt.mnt_flags |= MNT_READONLY;
  384. /*
  385. * MNT_READONLY must become visible before ~MNT_WRITE_HOLD, so writers
  386. * that become unheld will see MNT_READONLY.
  387. */
  388. smp_wmb();
  389. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  390. br_write_unlock(vfsmount_lock);
  391. return ret;
  392. }
  393. static void __mnt_unmake_readonly(struct mount *mnt)
  394. {
  395. br_write_lock(vfsmount_lock);
  396. mnt->mnt.mnt_flags &= ~MNT_READONLY;
  397. br_write_unlock(vfsmount_lock);
  398. }
  399. int sb_prepare_remount_readonly(struct super_block *sb)
  400. {
  401. struct mount *mnt;
  402. int err = 0;
  403. /* Racy optimization. Recheck the counter under MNT_WRITE_HOLD */
  404. if (atomic_long_read(&sb->s_remove_count))
  405. return -EBUSY;
  406. br_write_lock(vfsmount_lock);
  407. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  408. if (!(mnt->mnt.mnt_flags & MNT_READONLY)) {
  409. mnt->mnt.mnt_flags |= MNT_WRITE_HOLD;
  410. smp_mb();
  411. if (mnt_get_writers(mnt) > 0) {
  412. err = -EBUSY;
  413. break;
  414. }
  415. }
  416. }
  417. if (!err && atomic_long_read(&sb->s_remove_count))
  418. err = -EBUSY;
  419. if (!err) {
  420. sb->s_readonly_remount = 1;
  421. smp_wmb();
  422. }
  423. list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
  424. if (mnt->mnt.mnt_flags & MNT_WRITE_HOLD)
  425. mnt->mnt.mnt_flags &= ~MNT_WRITE_HOLD;
  426. }
  427. br_write_unlock(vfsmount_lock);
  428. return err;
  429. }
  430. static void free_vfsmnt(struct mount *mnt)
  431. {
  432. kfree(mnt->mnt_devname);
  433. mnt_free_id(mnt);
  434. #ifdef CONFIG_SMP
  435. free_percpu(mnt->mnt_pcp);
  436. #endif
  437. kmem_cache_free(mnt_cache, mnt);
  438. }
  439. /*
  440. * find the first or last mount at @dentry on vfsmount @mnt depending on
  441. * @dir. If @dir is set return the first mount else return the last mount.
  442. * vfsmount_lock must be held for read or write.
  443. */
  444. struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
  445. int dir)
  446. {
  447. struct list_head *head = mount_hashtable + hash(mnt, dentry);
  448. struct list_head *tmp = head;
  449. struct mount *p, *found = NULL;
  450. for (;;) {
  451. tmp = dir ? tmp->next : tmp->prev;
  452. p = NULL;
  453. if (tmp == head)
  454. break;
  455. p = list_entry(tmp, struct mount, mnt_hash);
  456. if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry) {
  457. found = p;
  458. break;
  459. }
  460. }
  461. return found;
  462. }
  463. /*
  464. * lookup_mnt increments the ref count before returning
  465. * the vfsmount struct.
  466. */
  467. struct vfsmount *lookup_mnt(struct path *path)
  468. {
  469. struct mount *child_mnt;
  470. br_read_lock(vfsmount_lock);
  471. child_mnt = __lookup_mnt(path->mnt, path->dentry, 1);
  472. if (child_mnt) {
  473. mnt_add_count(child_mnt, 1);
  474. br_read_unlock(vfsmount_lock);
  475. return &child_mnt->mnt;
  476. } else {
  477. br_read_unlock(vfsmount_lock);
  478. return NULL;
  479. }
  480. }
  481. static inline int check_mnt(struct mount *mnt)
  482. {
  483. return mnt->mnt_ns == current->nsproxy->mnt_ns;
  484. }
  485. /*
  486. * vfsmount lock must be held for write
  487. */
  488. static void touch_mnt_namespace(struct mnt_namespace *ns)
  489. {
  490. if (ns) {
  491. ns->event = ++event;
  492. wake_up_interruptible(&ns->poll);
  493. }
  494. }
  495. /*
  496. * vfsmount lock must be held for write
  497. */
  498. static void __touch_mnt_namespace(struct mnt_namespace *ns)
  499. {
  500. if (ns && ns->event != event) {
  501. ns->event = event;
  502. wake_up_interruptible(&ns->poll);
  503. }
  504. }
  505. /*
  506. * Clear dentry's mounted state if it has no remaining mounts.
  507. * vfsmount_lock must be held for write.
  508. */
  509. static void dentry_reset_mounted(struct dentry *dentry)
  510. {
  511. unsigned u;
  512. for (u = 0; u < HASH_SIZE; u++) {
  513. struct mount *p;
  514. list_for_each_entry(p, &mount_hashtable[u], mnt_hash) {
  515. if (p->mnt_mountpoint == dentry)
  516. return;
  517. }
  518. }
  519. spin_lock(&dentry->d_lock);
  520. dentry->d_flags &= ~DCACHE_MOUNTED;
  521. spin_unlock(&dentry->d_lock);
  522. }
  523. /*
  524. * vfsmount lock must be held for write
  525. */
  526. static void detach_mnt(struct mount *mnt, struct path *old_path)
  527. {
  528. old_path->dentry = mnt->mnt_mountpoint;
  529. old_path->mnt = &mnt->mnt_parent->mnt;
  530. mnt->mnt_parent = mnt;
  531. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  532. list_del_init(&mnt->mnt_child);
  533. list_del_init(&mnt->mnt_hash);
  534. dentry_reset_mounted(old_path->dentry);
  535. }
  536. /*
  537. * vfsmount lock must be held for write
  538. */
  539. void mnt_set_mountpoint(struct mount *mnt, struct dentry *dentry,
  540. struct mount *child_mnt)
  541. {
  542. mnt_add_count(mnt, 1); /* essentially, that's mntget */
  543. child_mnt->mnt_mountpoint = dget(dentry);
  544. child_mnt->mnt_parent = mnt;
  545. spin_lock(&dentry->d_lock);
  546. dentry->d_flags |= DCACHE_MOUNTED;
  547. spin_unlock(&dentry->d_lock);
  548. }
  549. /*
  550. * vfsmount lock must be held for write
  551. */
  552. static void attach_mnt(struct mount *mnt, struct path *path)
  553. {
  554. mnt_set_mountpoint(real_mount(path->mnt), path->dentry, mnt);
  555. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  556. hash(path->mnt, path->dentry));
  557. list_add_tail(&mnt->mnt_child, &real_mount(path->mnt)->mnt_mounts);
  558. }
  559. static inline void __mnt_make_longterm(struct mount *mnt)
  560. {
  561. #ifdef CONFIG_SMP
  562. atomic_inc(&mnt->mnt_longterm);
  563. #endif
  564. }
  565. /* needs vfsmount lock for write */
  566. static inline void __mnt_make_shortterm(struct mount *mnt)
  567. {
  568. #ifdef CONFIG_SMP
  569. atomic_dec(&mnt->mnt_longterm);
  570. #endif
  571. }
  572. /*
  573. * vfsmount lock must be held for write
  574. */
  575. static void commit_tree(struct mount *mnt)
  576. {
  577. struct mount *parent = mnt->mnt_parent;
  578. struct mount *m;
  579. LIST_HEAD(head);
  580. struct mnt_namespace *n = parent->mnt_ns;
  581. BUG_ON(parent == mnt);
  582. list_add_tail(&head, &mnt->mnt_list);
  583. list_for_each_entry(m, &head, mnt_list) {
  584. m->mnt_ns = n;
  585. __mnt_make_longterm(m);
  586. }
  587. list_splice(&head, n->list.prev);
  588. list_add_tail(&mnt->mnt_hash, mount_hashtable +
  589. hash(&parent->mnt, mnt->mnt_mountpoint));
  590. list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
  591. touch_mnt_namespace(n);
  592. }
  593. static struct mount *next_mnt(struct mount *p, struct mount *root)
  594. {
  595. struct list_head *next = p->mnt_mounts.next;
  596. if (next == &p->mnt_mounts) {
  597. while (1) {
  598. if (p == root)
  599. return NULL;
  600. next = p->mnt_child.next;
  601. if (next != &p->mnt_parent->mnt_mounts)
  602. break;
  603. p = p->mnt_parent;
  604. }
  605. }
  606. return list_entry(next, struct mount, mnt_child);
  607. }
  608. static struct mount *skip_mnt_tree(struct mount *p)
  609. {
  610. struct list_head *prev = p->mnt_mounts.prev;
  611. while (prev != &p->mnt_mounts) {
  612. p = list_entry(prev, struct mount, mnt_child);
  613. prev = p->mnt_mounts.prev;
  614. }
  615. return p;
  616. }
  617. struct vfsmount *
  618. vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
  619. {
  620. struct mount *mnt;
  621. struct dentry *root;
  622. if (!type)
  623. return ERR_PTR(-ENODEV);
  624. mnt = alloc_vfsmnt(name);
  625. if (!mnt)
  626. return ERR_PTR(-ENOMEM);
  627. if (flags & MS_KERNMOUNT)
  628. mnt->mnt.mnt_flags = MNT_INTERNAL;
  629. root = mount_fs(type, flags, name, data);
  630. if (IS_ERR(root)) {
  631. free_vfsmnt(mnt);
  632. return ERR_CAST(root);
  633. }
  634. mnt->mnt.mnt_root = root;
  635. mnt->mnt.mnt_sb = root->d_sb;
  636. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  637. mnt->mnt_parent = mnt;
  638. br_write_lock(vfsmount_lock);
  639. list_add_tail(&mnt->mnt_instance, &root->d_sb->s_mounts);
  640. br_write_unlock(vfsmount_lock);
  641. return &mnt->mnt;
  642. }
  643. EXPORT_SYMBOL_GPL(vfs_kern_mount);
  644. static struct mount *clone_mnt(struct mount *old, struct dentry *root,
  645. int flag)
  646. {
  647. struct super_block *sb = old->mnt.mnt_sb;
  648. struct mount *mnt = alloc_vfsmnt(old->mnt_devname);
  649. if (mnt) {
  650. if (flag & (CL_SLAVE | CL_PRIVATE))
  651. mnt->mnt_group_id = 0; /* not a peer of original */
  652. else
  653. mnt->mnt_group_id = old->mnt_group_id;
  654. if ((flag & CL_MAKE_SHARED) && !mnt->mnt_group_id) {
  655. int err = mnt_alloc_group_id(mnt);
  656. if (err)
  657. goto out_free;
  658. }
  659. mnt->mnt.mnt_flags = old->mnt.mnt_flags & ~MNT_WRITE_HOLD;
  660. atomic_inc(&sb->s_active);
  661. mnt->mnt.mnt_sb = sb;
  662. mnt->mnt.mnt_root = dget(root);
  663. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  664. mnt->mnt_parent = mnt;
  665. br_write_lock(vfsmount_lock);
  666. list_add_tail(&mnt->mnt_instance, &sb->s_mounts);
  667. br_write_unlock(vfsmount_lock);
  668. if (flag & CL_SLAVE) {
  669. list_add(&mnt->mnt_slave, &old->mnt_slave_list);
  670. mnt->mnt_master = old;
  671. CLEAR_MNT_SHARED(mnt);
  672. } else if (!(flag & CL_PRIVATE)) {
  673. if ((flag & CL_MAKE_SHARED) || IS_MNT_SHARED(old))
  674. list_add(&mnt->mnt_share, &old->mnt_share);
  675. if (IS_MNT_SLAVE(old))
  676. list_add(&mnt->mnt_slave, &old->mnt_slave);
  677. mnt->mnt_master = old->mnt_master;
  678. }
  679. if (flag & CL_MAKE_SHARED)
  680. set_mnt_shared(mnt);
  681. /* stick the duplicate mount on the same expiry list
  682. * as the original if that was on one */
  683. if (flag & CL_EXPIRE) {
  684. if (!list_empty(&old->mnt_expire))
  685. list_add(&mnt->mnt_expire, &old->mnt_expire);
  686. }
  687. }
  688. return mnt;
  689. out_free:
  690. free_vfsmnt(mnt);
  691. return NULL;
  692. }
  693. static inline void mntfree(struct mount *mnt)
  694. {
  695. struct vfsmount *m = &mnt->mnt;
  696. struct super_block *sb = m->mnt_sb;
  697. /*
  698. * This probably indicates that somebody messed
  699. * up a mnt_want/drop_write() pair. If this
  700. * happens, the filesystem was probably unable
  701. * to make r/w->r/o transitions.
  702. */
  703. /*
  704. * The locking used to deal with mnt_count decrement provides barriers,
  705. * so mnt_get_writers() below is safe.
  706. */
  707. WARN_ON(mnt_get_writers(mnt));
  708. fsnotify_vfsmount_delete(m);
  709. dput(m->mnt_root);
  710. free_vfsmnt(mnt);
  711. deactivate_super(sb);
  712. }
  713. static void mntput_no_expire(struct mount *mnt)
  714. {
  715. put_again:
  716. #ifdef CONFIG_SMP
  717. br_read_lock(vfsmount_lock);
  718. if (likely(atomic_read(&mnt->mnt_longterm))) {
  719. mnt_add_count(mnt, -1);
  720. br_read_unlock(vfsmount_lock);
  721. return;
  722. }
  723. br_read_unlock(vfsmount_lock);
  724. br_write_lock(vfsmount_lock);
  725. mnt_add_count(mnt, -1);
  726. if (mnt_get_count(mnt)) {
  727. br_write_unlock(vfsmount_lock);
  728. return;
  729. }
  730. #else
  731. mnt_add_count(mnt, -1);
  732. if (likely(mnt_get_count(mnt)))
  733. return;
  734. br_write_lock(vfsmount_lock);
  735. #endif
  736. if (unlikely(mnt->mnt_pinned)) {
  737. mnt_add_count(mnt, mnt->mnt_pinned + 1);
  738. mnt->mnt_pinned = 0;
  739. br_write_unlock(vfsmount_lock);
  740. acct_auto_close_mnt(&mnt->mnt);
  741. goto put_again;
  742. }
  743. list_del(&mnt->mnt_instance);
  744. br_write_unlock(vfsmount_lock);
  745. mntfree(mnt);
  746. }
  747. void mntput(struct vfsmount *mnt)
  748. {
  749. if (mnt) {
  750. struct mount *m = real_mount(mnt);
  751. /* avoid cacheline pingpong, hope gcc doesn't get "smart" */
  752. if (unlikely(m->mnt_expiry_mark))
  753. m->mnt_expiry_mark = 0;
  754. mntput_no_expire(m);
  755. }
  756. }
  757. EXPORT_SYMBOL(mntput);
  758. struct vfsmount *mntget(struct vfsmount *mnt)
  759. {
  760. if (mnt)
  761. mnt_add_count(real_mount(mnt), 1);
  762. return mnt;
  763. }
  764. EXPORT_SYMBOL(mntget);
  765. void mnt_pin(struct vfsmount *mnt)
  766. {
  767. br_write_lock(vfsmount_lock);
  768. real_mount(mnt)->mnt_pinned++;
  769. br_write_unlock(vfsmount_lock);
  770. }
  771. EXPORT_SYMBOL(mnt_pin);
  772. void mnt_unpin(struct vfsmount *m)
  773. {
  774. struct mount *mnt = real_mount(m);
  775. br_write_lock(vfsmount_lock);
  776. if (mnt->mnt_pinned) {
  777. mnt_add_count(mnt, 1);
  778. mnt->mnt_pinned--;
  779. }
  780. br_write_unlock(vfsmount_lock);
  781. }
  782. EXPORT_SYMBOL(mnt_unpin);
  783. static inline void mangle(struct seq_file *m, const char *s)
  784. {
  785. seq_escape(m, s, " \t\n\\");
  786. }
  787. /*
  788. * Simple .show_options callback for filesystems which don't want to
  789. * implement more complex mount option showing.
  790. *
  791. * See also save_mount_options().
  792. */
  793. int generic_show_options(struct seq_file *m, struct dentry *root)
  794. {
  795. const char *options;
  796. rcu_read_lock();
  797. options = rcu_dereference(root->d_sb->s_options);
  798. if (options != NULL && options[0]) {
  799. seq_putc(m, ',');
  800. mangle(m, options);
  801. }
  802. rcu_read_unlock();
  803. return 0;
  804. }
  805. EXPORT_SYMBOL(generic_show_options);
  806. /*
  807. * If filesystem uses generic_show_options(), this function should be
  808. * called from the fill_super() callback.
  809. *
  810. * The .remount_fs callback usually needs to be handled in a special
  811. * way, to make sure, that previous options are not overwritten if the
  812. * remount fails.
  813. *
  814. * Also note, that if the filesystem's .remount_fs function doesn't
  815. * reset all options to their default value, but changes only newly
  816. * given options, then the displayed options will not reflect reality
  817. * any more.
  818. */
  819. void save_mount_options(struct super_block *sb, char *options)
  820. {
  821. BUG_ON(sb->s_options);
  822. rcu_assign_pointer(sb->s_options, kstrdup(options, GFP_KERNEL));
  823. }
  824. EXPORT_SYMBOL(save_mount_options);
  825. void replace_mount_options(struct super_block *sb, char *options)
  826. {
  827. char *old = sb->s_options;
  828. rcu_assign_pointer(sb->s_options, options);
  829. if (old) {
  830. synchronize_rcu();
  831. kfree(old);
  832. }
  833. }
  834. EXPORT_SYMBOL(replace_mount_options);
  835. #ifdef CONFIG_PROC_FS
  836. /* iterator; we want it to have access to namespace_sem, thus here... */
  837. static void *m_start(struct seq_file *m, loff_t *pos)
  838. {
  839. struct proc_mounts *p = container_of(m, struct proc_mounts, m);
  840. down_read(&namespace_sem);
  841. return seq_list_start(&p->ns->list, *pos);
  842. }
  843. static void *m_next(struct seq_file *m, void *v, loff_t *pos)
  844. {
  845. struct proc_mounts *p = container_of(m, struct proc_mounts, m);
  846. return seq_list_next(v, &p->ns->list, pos);
  847. }
  848. static void m_stop(struct seq_file *m, void *v)
  849. {
  850. up_read(&namespace_sem);
  851. }
  852. static int m_show(struct seq_file *m, void *v)
  853. {
  854. struct proc_mounts *p = container_of(m, struct proc_mounts, m);
  855. struct mount *r = list_entry(v, struct mount, mnt_list);
  856. return p->show(m, &r->mnt);
  857. }
  858. const struct seq_operations mounts_op = {
  859. .start = m_start,
  860. .next = m_next,
  861. .stop = m_stop,
  862. .show = m_show,
  863. };
  864. #endif /* CONFIG_PROC_FS */
  865. /**
  866. * may_umount_tree - check if a mount tree is busy
  867. * @mnt: root of mount tree
  868. *
  869. * This is called to check if a tree of mounts has any
  870. * open files, pwds, chroots or sub mounts that are
  871. * busy.
  872. */
  873. int may_umount_tree(struct vfsmount *m)
  874. {
  875. struct mount *mnt = real_mount(m);
  876. int actual_refs = 0;
  877. int minimum_refs = 0;
  878. struct mount *p;
  879. BUG_ON(!m);
  880. /* write lock needed for mnt_get_count */
  881. br_write_lock(vfsmount_lock);
  882. for (p = mnt; p; p = next_mnt(p, mnt)) {
  883. actual_refs += mnt_get_count(p);
  884. minimum_refs += 2;
  885. }
  886. br_write_unlock(vfsmount_lock);
  887. if (actual_refs > minimum_refs)
  888. return 0;
  889. return 1;
  890. }
  891. EXPORT_SYMBOL(may_umount_tree);
  892. /**
  893. * may_umount - check if a mount point is busy
  894. * @mnt: root of mount
  895. *
  896. * This is called to check if a mount point has any
  897. * open files, pwds, chroots or sub mounts. If the
  898. * mount has sub mounts this will return busy
  899. * regardless of whether the sub mounts are busy.
  900. *
  901. * Doesn't take quota and stuff into account. IOW, in some cases it will
  902. * give false negatives. The main reason why it's here is that we need
  903. * a non-destructive way to look for easily umountable filesystems.
  904. */
  905. int may_umount(struct vfsmount *mnt)
  906. {
  907. int ret = 1;
  908. down_read(&namespace_sem);
  909. br_write_lock(vfsmount_lock);
  910. if (propagate_mount_busy(real_mount(mnt), 2))
  911. ret = 0;
  912. br_write_unlock(vfsmount_lock);
  913. up_read(&namespace_sem);
  914. return ret;
  915. }
  916. EXPORT_SYMBOL(may_umount);
  917. void release_mounts(struct list_head *head)
  918. {
  919. struct mount *mnt;
  920. while (!list_empty(head)) {
  921. mnt = list_first_entry(head, struct mount, mnt_hash);
  922. list_del_init(&mnt->mnt_hash);
  923. if (mnt_has_parent(mnt)) {
  924. struct dentry *dentry;
  925. struct mount *m;
  926. br_write_lock(vfsmount_lock);
  927. dentry = mnt->mnt_mountpoint;
  928. m = mnt->mnt_parent;
  929. mnt->mnt_mountpoint = mnt->mnt.mnt_root;
  930. mnt->mnt_parent = mnt;
  931. m->mnt_ghosts--;
  932. br_write_unlock(vfsmount_lock);
  933. dput(dentry);
  934. mntput(&m->mnt);
  935. }
  936. mntput(&mnt->mnt);
  937. }
  938. }
  939. /*
  940. * vfsmount lock must be held for write
  941. * namespace_sem must be held for write
  942. */
  943. void umount_tree(struct mount *mnt, int propagate, struct list_head *kill)
  944. {
  945. LIST_HEAD(tmp_list);
  946. struct mount *p;
  947. for (p = mnt; p; p = next_mnt(p, mnt))
  948. list_move(&p->mnt_hash, &tmp_list);
  949. if (propagate)
  950. propagate_umount(&tmp_list);
  951. list_for_each_entry(p, &tmp_list, mnt_hash) {
  952. list_del_init(&p->mnt_expire);
  953. list_del_init(&p->mnt_list);
  954. __touch_mnt_namespace(p->mnt_ns);
  955. p->mnt_ns = NULL;
  956. __mnt_make_shortterm(p);
  957. list_del_init(&p->mnt_child);
  958. if (mnt_has_parent(p)) {
  959. p->mnt_parent->mnt_ghosts++;
  960. dentry_reset_mounted(p->mnt_mountpoint);
  961. }
  962. change_mnt_propagation(p, MS_PRIVATE);
  963. }
  964. list_splice(&tmp_list, kill);
  965. }
  966. static void shrink_submounts(struct mount *mnt, struct list_head *umounts);
  967. static int do_umount(struct mount *mnt, int flags)
  968. {
  969. struct super_block *sb = mnt->mnt.mnt_sb;
  970. int retval;
  971. LIST_HEAD(umount_list);
  972. retval = security_sb_umount(&mnt->mnt, flags);
  973. if (retval)
  974. return retval;
  975. /*
  976. * Allow userspace to request a mountpoint be expired rather than
  977. * unmounting unconditionally. Unmount only happens if:
  978. * (1) the mark is already set (the mark is cleared by mntput())
  979. * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
  980. */
  981. if (flags & MNT_EXPIRE) {
  982. if (&mnt->mnt == current->fs->root.mnt ||
  983. flags & (MNT_FORCE | MNT_DETACH))
  984. return -EINVAL;
  985. /*
  986. * probably don't strictly need the lock here if we examined
  987. * all race cases, but it's a slowpath.
  988. */
  989. br_write_lock(vfsmount_lock);
  990. if (mnt_get_count(mnt) != 2) {
  991. br_write_unlock(vfsmount_lock);
  992. return -EBUSY;
  993. }
  994. br_write_unlock(vfsmount_lock);
  995. if (!xchg(&mnt->mnt_expiry_mark, 1))
  996. return -EAGAIN;
  997. }
  998. /*
  999. * If we may have to abort operations to get out of this
  1000. * mount, and they will themselves hold resources we must
  1001. * allow the fs to do things. In the Unix tradition of
  1002. * 'Gee thats tricky lets do it in userspace' the umount_begin
  1003. * might fail to complete on the first run through as other tasks
  1004. * must return, and the like. Thats for the mount program to worry
  1005. * about for the moment.
  1006. */
  1007. if (flags & MNT_FORCE && sb->s_op->umount_begin) {
  1008. sb->s_op->umount_begin(sb);
  1009. }
  1010. /*
  1011. * No sense to grab the lock for this test, but test itself looks
  1012. * somewhat bogus. Suggestions for better replacement?
  1013. * Ho-hum... In principle, we might treat that as umount + switch
  1014. * to rootfs. GC would eventually take care of the old vfsmount.
  1015. * Actually it makes sense, especially if rootfs would contain a
  1016. * /reboot - static binary that would close all descriptors and
  1017. * call reboot(9). Then init(8) could umount root and exec /reboot.
  1018. */
  1019. if (&mnt->mnt == current->fs->root.mnt && !(flags & MNT_DETACH)) {
  1020. /*
  1021. * Special case for "unmounting" root ...
  1022. * we just try to remount it readonly.
  1023. */
  1024. down_write(&sb->s_umount);
  1025. if (!(sb->s_flags & MS_RDONLY))
  1026. retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
  1027. up_write(&sb->s_umount);
  1028. return retval;
  1029. }
  1030. down_write(&namespace_sem);
  1031. br_write_lock(vfsmount_lock);
  1032. event++;
  1033. if (!(flags & MNT_DETACH))
  1034. shrink_submounts(mnt, &umount_list);
  1035. retval = -EBUSY;
  1036. if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
  1037. if (!list_empty(&mnt->mnt_list))
  1038. umount_tree(mnt, 1, &umount_list);
  1039. retval = 0;
  1040. }
  1041. br_write_unlock(vfsmount_lock);
  1042. up_write(&namespace_sem);
  1043. release_mounts(&umount_list);
  1044. return retval;
  1045. }
  1046. /*
  1047. * Now umount can handle mount points as well as block devices.
  1048. * This is important for filesystems which use unnamed block devices.
  1049. *
  1050. * We now support a flag for forced unmount like the other 'big iron'
  1051. * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
  1052. */
  1053. SYSCALL_DEFINE2(umount, char __user *, name, int, flags)
  1054. {
  1055. struct path path;
  1056. struct mount *mnt;
  1057. int retval;
  1058. int lookup_flags = 0;
  1059. if (flags & ~(MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW))
  1060. return -EINVAL;
  1061. if (!(flags & UMOUNT_NOFOLLOW))
  1062. lookup_flags |= LOOKUP_FOLLOW;
  1063. retval = user_path_at(AT_FDCWD, name, lookup_flags, &path);
  1064. if (retval)
  1065. goto out;
  1066. mnt = real_mount(path.mnt);
  1067. retval = -EINVAL;
  1068. if (path.dentry != path.mnt->mnt_root)
  1069. goto dput_and_out;
  1070. if (!check_mnt(mnt))
  1071. goto dput_and_out;
  1072. retval = -EPERM;
  1073. if (!capable(CAP_SYS_ADMIN))
  1074. goto dput_and_out;
  1075. retval = do_umount(mnt, flags);
  1076. dput_and_out:
  1077. /* we mustn't call path_put() as that would clear mnt_expiry_mark */
  1078. dput(path.dentry);
  1079. mntput_no_expire(mnt);
  1080. out:
  1081. return retval;
  1082. }
  1083. #ifdef __ARCH_WANT_SYS_OLDUMOUNT
  1084. /*
  1085. * The 2.0 compatible umount. No flags.
  1086. */
  1087. SYSCALL_DEFINE1(oldumount, char __user *, name)
  1088. {
  1089. return sys_umount(name, 0);
  1090. }
  1091. #endif
  1092. static int mount_is_safe(struct path *path)
  1093. {
  1094. if (capable(CAP_SYS_ADMIN))
  1095. return 0;
  1096. return -EPERM;
  1097. #ifdef notyet
  1098. if (S_ISLNK(path->dentry->d_inode->i_mode))
  1099. return -EPERM;
  1100. if (path->dentry->d_inode->i_mode & S_ISVTX) {
  1101. if (current_uid() != path->dentry->d_inode->i_uid)
  1102. return -EPERM;
  1103. }
  1104. if (inode_permission(path->dentry->d_inode, MAY_WRITE))
  1105. return -EPERM;
  1106. return 0;
  1107. #endif
  1108. }
  1109. struct mount *copy_tree(struct mount *mnt, struct dentry *dentry,
  1110. int flag)
  1111. {
  1112. struct mount *res, *p, *q, *r;
  1113. struct path path;
  1114. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
  1115. return NULL;
  1116. res = q = clone_mnt(mnt, dentry, flag);
  1117. if (!q)
  1118. goto Enomem;
  1119. q->mnt_mountpoint = mnt->mnt_mountpoint;
  1120. p = mnt;
  1121. list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
  1122. struct mount *s;
  1123. if (!is_subdir(r->mnt_mountpoint, dentry))
  1124. continue;
  1125. for (s = r; s; s = next_mnt(s, r)) {
  1126. if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
  1127. s = skip_mnt_tree(s);
  1128. continue;
  1129. }
  1130. while (p != s->mnt_parent) {
  1131. p = p->mnt_parent;
  1132. q = q->mnt_parent;
  1133. }
  1134. p = s;
  1135. path.mnt = &q->mnt;
  1136. path.dentry = p->mnt_mountpoint;
  1137. q = clone_mnt(p, p->mnt.mnt_root, flag);
  1138. if (!q)
  1139. goto Enomem;
  1140. br_write_lock(vfsmount_lock);
  1141. list_add_tail(&q->mnt_list, &res->mnt_list);
  1142. attach_mnt(q, &path);
  1143. br_write_unlock(vfsmount_lock);
  1144. }
  1145. }
  1146. return res;
  1147. Enomem:
  1148. if (res) {
  1149. LIST_HEAD(umount_list);
  1150. br_write_lock(vfsmount_lock);
  1151. umount_tree(res, 0, &umount_list);
  1152. br_write_unlock(vfsmount_lock);
  1153. release_mounts(&umount_list);
  1154. }
  1155. return NULL;
  1156. }
  1157. struct vfsmount *collect_mounts(struct path *path)
  1158. {
  1159. struct mount *tree;
  1160. down_write(&namespace_sem);
  1161. tree = copy_tree(real_mount(path->mnt), path->dentry,
  1162. CL_COPY_ALL | CL_PRIVATE);
  1163. up_write(&namespace_sem);
  1164. return tree ? &tree->mnt : NULL;
  1165. }
  1166. void drop_collected_mounts(struct vfsmount *mnt)
  1167. {
  1168. LIST_HEAD(umount_list);
  1169. down_write(&namespace_sem);
  1170. br_write_lock(vfsmount_lock);
  1171. umount_tree(real_mount(mnt), 0, &umount_list);
  1172. br_write_unlock(vfsmount_lock);
  1173. up_write(&namespace_sem);
  1174. release_mounts(&umount_list);
  1175. }
  1176. int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
  1177. struct vfsmount *root)
  1178. {
  1179. struct mount *mnt;
  1180. int res = f(root, arg);
  1181. if (res)
  1182. return res;
  1183. list_for_each_entry(mnt, &real_mount(root)->mnt_list, mnt_list) {
  1184. res = f(&mnt->mnt, arg);
  1185. if (res)
  1186. return res;
  1187. }
  1188. return 0;
  1189. }
  1190. static void cleanup_group_ids(struct mount *mnt, struct mount *end)
  1191. {
  1192. struct mount *p;
  1193. for (p = mnt; p != end; p = next_mnt(p, mnt)) {
  1194. if (p->mnt_group_id && !IS_MNT_SHARED(p))
  1195. mnt_release_group_id(p);
  1196. }
  1197. }
  1198. static int invent_group_ids(struct mount *mnt, bool recurse)
  1199. {
  1200. struct mount *p;
  1201. for (p = mnt; p; p = recurse ? next_mnt(p, mnt) : NULL) {
  1202. if (!p->mnt_group_id && !IS_MNT_SHARED(p)) {
  1203. int err = mnt_alloc_group_id(p);
  1204. if (err) {
  1205. cleanup_group_ids(mnt, p);
  1206. return err;
  1207. }
  1208. }
  1209. }
  1210. return 0;
  1211. }
  1212. /*
  1213. * @source_mnt : mount tree to be attached
  1214. * @nd : place the mount tree @source_mnt is attached
  1215. * @parent_nd : if non-null, detach the source_mnt from its parent and
  1216. * store the parent mount and mountpoint dentry.
  1217. * (done when source_mnt is moved)
  1218. *
  1219. * NOTE: in the table below explains the semantics when a source mount
  1220. * of a given type is attached to a destination mount of a given type.
  1221. * ---------------------------------------------------------------------------
  1222. * | BIND MOUNT OPERATION |
  1223. * |**************************************************************************
  1224. * | source-->| shared | private | slave | unbindable |
  1225. * | dest | | | | |
  1226. * | | | | | | |
  1227. * | v | | | | |
  1228. * |**************************************************************************
  1229. * | shared | shared (++) | shared (+) | shared(+++)| invalid |
  1230. * | | | | | |
  1231. * |non-shared| shared (+) | private | slave (*) | invalid |
  1232. * ***************************************************************************
  1233. * A bind operation clones the source mount and mounts the clone on the
  1234. * destination mount.
  1235. *
  1236. * (++) the cloned mount is propagated to all the mounts in the propagation
  1237. * tree of the destination mount and the cloned mount is added to
  1238. * the peer group of the source mount.
  1239. * (+) the cloned mount is created under the destination mount and is marked
  1240. * as shared. The cloned mount is added to the peer group of the source
  1241. * mount.
  1242. * (+++) the mount is propagated to all the mounts in the propagation tree
  1243. * of the destination mount and the cloned mount is made slave
  1244. * of the same master as that of the source mount. The cloned mount
  1245. * is marked as 'shared and slave'.
  1246. * (*) the cloned mount is made a slave of the same master as that of the
  1247. * source mount.
  1248. *
  1249. * ---------------------------------------------------------------------------
  1250. * | MOVE MOUNT OPERATION |
  1251. * |**************************************************************************
  1252. * | source-->| shared | private | slave | unbindable |
  1253. * | dest | | | | |
  1254. * | | | | | | |
  1255. * | v | | | | |
  1256. * |**************************************************************************
  1257. * | shared | shared (+) | shared (+) | shared(+++) | invalid |
  1258. * | | | | | |
  1259. * |non-shared| shared (+*) | private | slave (*) | unbindable |
  1260. * ***************************************************************************
  1261. *
  1262. * (+) the mount is moved to the destination. And is then propagated to
  1263. * all the mounts in the propagation tree of the destination mount.
  1264. * (+*) the mount is moved to the destination.
  1265. * (+++) the mount is moved to the destination and is then propagated to
  1266. * all the mounts belonging to the destination mount's propagation tree.
  1267. * the mount is marked as 'shared and slave'.
  1268. * (*) the mount continues to be a slave at the new location.
  1269. *
  1270. * if the source mount is a tree, the operations explained above is
  1271. * applied to each mount in the tree.
  1272. * Must be called without spinlocks held, since this function can sleep
  1273. * in allocations.
  1274. */
  1275. static int attach_recursive_mnt(struct mount *source_mnt,
  1276. struct path *path, struct path *parent_path)
  1277. {
  1278. LIST_HEAD(tree_list);
  1279. struct mount *dest_mnt = real_mount(path->mnt);
  1280. struct dentry *dest_dentry = path->dentry;
  1281. struct mount *child, *p;
  1282. int err;
  1283. if (IS_MNT_SHARED(dest_mnt)) {
  1284. err = invent_group_ids(source_mnt, true);
  1285. if (err)
  1286. goto out;
  1287. }
  1288. err = propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list);
  1289. if (err)
  1290. goto out_cleanup_ids;
  1291. br_write_lock(vfsmount_lock);
  1292. if (IS_MNT_SHARED(dest_mnt)) {
  1293. for (p = source_mnt; p; p = next_mnt(p, source_mnt))
  1294. set_mnt_shared(p);
  1295. }
  1296. if (parent_path) {
  1297. detach_mnt(source_mnt, parent_path);
  1298. attach_mnt(source_mnt, path);
  1299. touch_mnt_namespace(source_mnt->mnt_ns);
  1300. } else {
  1301. mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
  1302. commit_tree(source_mnt);
  1303. }
  1304. list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
  1305. list_del_init(&child->mnt_hash);
  1306. commit_tree(child);
  1307. }
  1308. br_write_unlock(vfsmount_lock);
  1309. return 0;
  1310. out_cleanup_ids:
  1311. if (IS_MNT_SHARED(dest_mnt))
  1312. cleanup_group_ids(source_mnt, NULL);
  1313. out:
  1314. return err;
  1315. }
  1316. static int lock_mount(struct path *path)
  1317. {
  1318. struct vfsmount *mnt;
  1319. retry:
  1320. mutex_lock(&path->dentry->d_inode->i_mutex);
  1321. if (unlikely(cant_mount(path->dentry))) {
  1322. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1323. return -ENOENT;
  1324. }
  1325. down_write(&namespace_sem);
  1326. mnt = lookup_mnt(path);
  1327. if (likely(!mnt))
  1328. return 0;
  1329. up_write(&namespace_sem);
  1330. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1331. path_put(path);
  1332. path->mnt = mnt;
  1333. path->dentry = dget(mnt->mnt_root);
  1334. goto retry;
  1335. }
  1336. static void unlock_mount(struct path *path)
  1337. {
  1338. up_write(&namespace_sem);
  1339. mutex_unlock(&path->dentry->d_inode->i_mutex);
  1340. }
  1341. static int graft_tree(struct mount *mnt, struct path *path)
  1342. {
  1343. if (mnt->mnt.mnt_sb->s_flags & MS_NOUSER)
  1344. return -EINVAL;
  1345. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1346. S_ISDIR(mnt->mnt.mnt_root->d_inode->i_mode))
  1347. return -ENOTDIR;
  1348. if (d_unlinked(path->dentry))
  1349. return -ENOENT;
  1350. return attach_recursive_mnt(mnt, path, NULL);
  1351. }
  1352. /*
  1353. * Sanity check the flags to change_mnt_propagation.
  1354. */
  1355. static int flags_to_propagation_type(int flags)
  1356. {
  1357. int type = flags & ~(MS_REC | MS_SILENT);
  1358. /* Fail if any non-propagation flags are set */
  1359. if (type & ~(MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1360. return 0;
  1361. /* Only one propagation flag should be set */
  1362. if (!is_power_of_2(type))
  1363. return 0;
  1364. return type;
  1365. }
  1366. /*
  1367. * recursively change the type of the mountpoint.
  1368. */
  1369. static int do_change_type(struct path *path, int flag)
  1370. {
  1371. struct mount *m;
  1372. struct mount *mnt = real_mount(path->mnt);
  1373. int recurse = flag & MS_REC;
  1374. int type;
  1375. int err = 0;
  1376. if (!capable(CAP_SYS_ADMIN))
  1377. return -EPERM;
  1378. if (path->dentry != path->mnt->mnt_root)
  1379. return -EINVAL;
  1380. type = flags_to_propagation_type(flag);
  1381. if (!type)
  1382. return -EINVAL;
  1383. down_write(&namespace_sem);
  1384. if (type == MS_SHARED) {
  1385. err = invent_group_ids(mnt, recurse);
  1386. if (err)
  1387. goto out_unlock;
  1388. }
  1389. br_write_lock(vfsmount_lock);
  1390. for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
  1391. change_mnt_propagation(m, type);
  1392. br_write_unlock(vfsmount_lock);
  1393. out_unlock:
  1394. up_write(&namespace_sem);
  1395. return err;
  1396. }
  1397. /*
  1398. * do loopback mount.
  1399. */
  1400. static int do_loopback(struct path *path, char *old_name,
  1401. int recurse)
  1402. {
  1403. LIST_HEAD(umount_list);
  1404. struct path old_path;
  1405. struct mount *mnt = NULL, *old;
  1406. int err = mount_is_safe(path);
  1407. if (err)
  1408. return err;
  1409. if (!old_name || !*old_name)
  1410. return -EINVAL;
  1411. err = kern_path(old_name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &old_path);
  1412. if (err)
  1413. return err;
  1414. err = lock_mount(path);
  1415. if (err)
  1416. goto out;
  1417. old = real_mount(old_path.mnt);
  1418. err = -EINVAL;
  1419. if (IS_MNT_UNBINDABLE(old))
  1420. goto out2;
  1421. if (!check_mnt(real_mount(path->mnt)) || !check_mnt(old))
  1422. goto out2;
  1423. err = -ENOMEM;
  1424. if (recurse)
  1425. mnt = copy_tree(old, old_path.dentry, 0);
  1426. else
  1427. mnt = clone_mnt(old, old_path.dentry, 0);
  1428. if (!mnt)
  1429. goto out2;
  1430. err = graft_tree(mnt, path);
  1431. if (err) {
  1432. br_write_lock(vfsmount_lock);
  1433. umount_tree(mnt, 0, &umount_list);
  1434. br_write_unlock(vfsmount_lock);
  1435. }
  1436. out2:
  1437. unlock_mount(path);
  1438. release_mounts(&umount_list);
  1439. out:
  1440. path_put(&old_path);
  1441. return err;
  1442. }
  1443. static int change_mount_flags(struct vfsmount *mnt, int ms_flags)
  1444. {
  1445. int error = 0;
  1446. int readonly_request = 0;
  1447. if (ms_flags & MS_RDONLY)
  1448. readonly_request = 1;
  1449. if (readonly_request == __mnt_is_readonly(mnt))
  1450. return 0;
  1451. if (readonly_request)
  1452. error = mnt_make_readonly(real_mount(mnt));
  1453. else
  1454. __mnt_unmake_readonly(real_mount(mnt));
  1455. return error;
  1456. }
  1457. /*
  1458. * change filesystem flags. dir should be a physical root of filesystem.
  1459. * If you've mounted a non-root directory somewhere and want to do remount
  1460. * on it - tough luck.
  1461. */
  1462. static int do_remount(struct path *path, int flags, int mnt_flags,
  1463. void *data)
  1464. {
  1465. int err;
  1466. struct super_block *sb = path->mnt->mnt_sb;
  1467. struct mount *mnt = real_mount(path->mnt);
  1468. if (!capable(CAP_SYS_ADMIN))
  1469. return -EPERM;
  1470. if (!check_mnt(mnt))
  1471. return -EINVAL;
  1472. if (path->dentry != path->mnt->mnt_root)
  1473. return -EINVAL;
  1474. err = security_sb_remount(sb, data);
  1475. if (err)
  1476. return err;
  1477. down_write(&sb->s_umount);
  1478. if (flags & MS_BIND)
  1479. err = change_mount_flags(path->mnt, flags);
  1480. else
  1481. err = do_remount_sb(sb, flags, data, 0);
  1482. if (!err) {
  1483. br_write_lock(vfsmount_lock);
  1484. mnt_flags |= mnt->mnt.mnt_flags & MNT_PROPAGATION_MASK;
  1485. mnt->mnt.mnt_flags = mnt_flags;
  1486. br_write_unlock(vfsmount_lock);
  1487. }
  1488. up_write(&sb->s_umount);
  1489. if (!err) {
  1490. br_write_lock(vfsmount_lock);
  1491. touch_mnt_namespace(mnt->mnt_ns);
  1492. br_write_unlock(vfsmount_lock);
  1493. }
  1494. return err;
  1495. }
  1496. static inline int tree_contains_unbindable(struct mount *mnt)
  1497. {
  1498. struct mount *p;
  1499. for (p = mnt; p; p = next_mnt(p, mnt)) {
  1500. if (IS_MNT_UNBINDABLE(p))
  1501. return 1;
  1502. }
  1503. return 0;
  1504. }
  1505. static int do_move_mount(struct path *path, char *old_name)
  1506. {
  1507. struct path old_path, parent_path;
  1508. struct mount *p;
  1509. struct mount *old;
  1510. int err = 0;
  1511. if (!capable(CAP_SYS_ADMIN))
  1512. return -EPERM;
  1513. if (!old_name || !*old_name)
  1514. return -EINVAL;
  1515. err = kern_path(old_name, LOOKUP_FOLLOW, &old_path);
  1516. if (err)
  1517. return err;
  1518. err = lock_mount(path);
  1519. if (err < 0)
  1520. goto out;
  1521. old = real_mount(old_path.mnt);
  1522. p = real_mount(path->mnt);
  1523. err = -EINVAL;
  1524. if (!check_mnt(p) || !check_mnt(old))
  1525. goto out1;
  1526. if (d_unlinked(path->dentry))
  1527. goto out1;
  1528. err = -EINVAL;
  1529. if (old_path.dentry != old_path.mnt->mnt_root)
  1530. goto out1;
  1531. if (!mnt_has_parent(old))
  1532. goto out1;
  1533. if (S_ISDIR(path->dentry->d_inode->i_mode) !=
  1534. S_ISDIR(old_path.dentry->d_inode->i_mode))
  1535. goto out1;
  1536. /*
  1537. * Don't move a mount residing in a shared parent.
  1538. */
  1539. if (IS_MNT_SHARED(old->mnt_parent))
  1540. goto out1;
  1541. /*
  1542. * Don't move a mount tree containing unbindable mounts to a destination
  1543. * mount which is shared.
  1544. */
  1545. if (IS_MNT_SHARED(p) && tree_contains_unbindable(old))
  1546. goto out1;
  1547. err = -ELOOP;
  1548. for (; mnt_has_parent(p); p = p->mnt_parent)
  1549. if (p == old)
  1550. goto out1;
  1551. err = attach_recursive_mnt(old, path, &parent_path);
  1552. if (err)
  1553. goto out1;
  1554. /* if the mount is moved, it should no longer be expire
  1555. * automatically */
  1556. list_del_init(&old->mnt_expire);
  1557. out1:
  1558. unlock_mount(path);
  1559. out:
  1560. if (!err)
  1561. path_put(&parent_path);
  1562. path_put(&old_path);
  1563. return err;
  1564. }
  1565. static struct vfsmount *fs_set_subtype(struct vfsmount *mnt, const char *fstype)
  1566. {
  1567. int err;
  1568. const char *subtype = strchr(fstype, '.');
  1569. if (subtype) {
  1570. subtype++;
  1571. err = -EINVAL;
  1572. if (!subtype[0])
  1573. goto err;
  1574. } else
  1575. subtype = "";
  1576. mnt->mnt_sb->s_subtype = kstrdup(subtype, GFP_KERNEL);
  1577. err = -ENOMEM;
  1578. if (!mnt->mnt_sb->s_subtype)
  1579. goto err;
  1580. return mnt;
  1581. err:
  1582. mntput(mnt);
  1583. return ERR_PTR(err);
  1584. }
  1585. static struct vfsmount *
  1586. do_kern_mount(const char *fstype, int flags, const char *name, void *data)
  1587. {
  1588. struct file_system_type *type = get_fs_type(fstype);
  1589. struct vfsmount *mnt;
  1590. if (!type)
  1591. return ERR_PTR(-ENODEV);
  1592. mnt = vfs_kern_mount(type, flags, name, data);
  1593. if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
  1594. !mnt->mnt_sb->s_subtype)
  1595. mnt = fs_set_subtype(mnt, fstype);
  1596. put_filesystem(type);
  1597. return mnt;
  1598. }
  1599. /*
  1600. * add a mount into a namespace's mount tree
  1601. */
  1602. static int do_add_mount(struct mount *newmnt, struct path *path, int mnt_flags)
  1603. {
  1604. int err;
  1605. mnt_flags &= ~(MNT_SHARED | MNT_WRITE_HOLD | MNT_INTERNAL);
  1606. err = lock_mount(path);
  1607. if (err)
  1608. return err;
  1609. err = -EINVAL;
  1610. if (!(mnt_flags & MNT_SHRINKABLE) && !check_mnt(real_mount(path->mnt)))
  1611. goto unlock;
  1612. /* Refuse the same filesystem on the same mount point */
  1613. err = -EBUSY;
  1614. if (path->mnt->mnt_sb == newmnt->mnt.mnt_sb &&
  1615. path->mnt->mnt_root == path->dentry)
  1616. goto unlock;
  1617. err = -EINVAL;
  1618. if (S_ISLNK(newmnt->mnt.mnt_root->d_inode->i_mode))
  1619. goto unlock;
  1620. newmnt->mnt.mnt_flags = mnt_flags;
  1621. err = graft_tree(newmnt, path);
  1622. unlock:
  1623. unlock_mount(path);
  1624. return err;
  1625. }
  1626. /*
  1627. * create a new mount for userspace and request it to be added into the
  1628. * namespace's tree
  1629. */
  1630. static int do_new_mount(struct path *path, char *type, int flags,
  1631. int mnt_flags, char *name, void *data)
  1632. {
  1633. struct vfsmount *mnt;
  1634. int err;
  1635. if (!type)
  1636. return -EINVAL;
  1637. /* we need capabilities... */
  1638. if (!capable(CAP_SYS_ADMIN))
  1639. return -EPERM;
  1640. mnt = do_kern_mount(type, flags, name, data);
  1641. if (IS_ERR(mnt))
  1642. return PTR_ERR(mnt);
  1643. err = do_add_mount(real_mount(mnt), path, mnt_flags);
  1644. if (err)
  1645. mntput(mnt);
  1646. return err;
  1647. }
  1648. int finish_automount(struct vfsmount *m, struct path *path)
  1649. {
  1650. struct mount *mnt = real_mount(m);
  1651. int err;
  1652. /* The new mount record should have at least 2 refs to prevent it being
  1653. * expired before we get a chance to add it
  1654. */
  1655. BUG_ON(mnt_get_count(mnt) < 2);
  1656. if (m->mnt_sb == path->mnt->mnt_sb &&
  1657. m->mnt_root == path->dentry) {
  1658. err = -ELOOP;
  1659. goto fail;
  1660. }
  1661. err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE);
  1662. if (!err)
  1663. return 0;
  1664. fail:
  1665. /* remove m from any expiration list it may be on */
  1666. if (!list_empty(&mnt->mnt_expire)) {
  1667. down_write(&namespace_sem);
  1668. br_write_lock(vfsmount_lock);
  1669. list_del_init(&mnt->mnt_expire);
  1670. br_write_unlock(vfsmount_lock);
  1671. up_write(&namespace_sem);
  1672. }
  1673. mntput(m);
  1674. mntput(m);
  1675. return err;
  1676. }
  1677. /**
  1678. * mnt_set_expiry - Put a mount on an expiration list
  1679. * @mnt: The mount to list.
  1680. * @expiry_list: The list to add the mount to.
  1681. */
  1682. void mnt_set_expiry(struct vfsmount *mnt, struct list_head *expiry_list)
  1683. {
  1684. down_write(&namespace_sem);
  1685. br_write_lock(vfsmount_lock);
  1686. list_add_tail(&real_mount(mnt)->mnt_expire, expiry_list);
  1687. br_write_unlock(vfsmount_lock);
  1688. up_write(&namespace_sem);
  1689. }
  1690. EXPORT_SYMBOL(mnt_set_expiry);
  1691. /*
  1692. * process a list of expirable mountpoints with the intent of discarding any
  1693. * mountpoints that aren't in use and haven't been touched since last we came
  1694. * here
  1695. */
  1696. void mark_mounts_for_expiry(struct list_head *mounts)
  1697. {
  1698. struct mount *mnt, *next;
  1699. LIST_HEAD(graveyard);
  1700. LIST_HEAD(umounts);
  1701. if (list_empty(mounts))
  1702. return;
  1703. down_write(&namespace_sem);
  1704. br_write_lock(vfsmount_lock);
  1705. /* extract from the expiration list every vfsmount that matches the
  1706. * following criteria:
  1707. * - only referenced by its parent vfsmount
  1708. * - still marked for expiry (marked on the last call here; marks are
  1709. * cleared by mntput())
  1710. */
  1711. list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
  1712. if (!xchg(&mnt->mnt_expiry_mark, 1) ||
  1713. propagate_mount_busy(mnt, 1))
  1714. continue;
  1715. list_move(&mnt->mnt_expire, &graveyard);
  1716. }
  1717. while (!list_empty(&graveyard)) {
  1718. mnt = list_first_entry(&graveyard, struct mount, mnt_expire);
  1719. touch_mnt_namespace(mnt->mnt_ns);
  1720. umount_tree(mnt, 1, &umounts);
  1721. }
  1722. br_write_unlock(vfsmount_lock);
  1723. up_write(&namespace_sem);
  1724. release_mounts(&umounts);
  1725. }
  1726. EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
  1727. /*
  1728. * Ripoff of 'select_parent()'
  1729. *
  1730. * search the list of submounts for a given mountpoint, and move any
  1731. * shrinkable submounts to the 'graveyard' list.
  1732. */
  1733. static int select_submounts(struct mount *parent, struct list_head *graveyard)
  1734. {
  1735. struct mount *this_parent = parent;
  1736. struct list_head *next;
  1737. int found = 0;
  1738. repeat:
  1739. next = this_parent->mnt_mounts.next;
  1740. resume:
  1741. while (next != &this_parent->mnt_mounts) {
  1742. struct list_head *tmp = next;
  1743. struct mount *mnt = list_entry(tmp, struct mount, mnt_child);
  1744. next = tmp->next;
  1745. if (!(mnt->mnt.mnt_flags & MNT_SHRINKABLE))
  1746. continue;
  1747. /*
  1748. * Descend a level if the d_mounts list is non-empty.
  1749. */
  1750. if (!list_empty(&mnt->mnt_mounts)) {
  1751. this_parent = mnt;
  1752. goto repeat;
  1753. }
  1754. if (!propagate_mount_busy(mnt, 1)) {
  1755. list_move_tail(&mnt->mnt_expire, graveyard);
  1756. found++;
  1757. }
  1758. }
  1759. /*
  1760. * All done at this level ... ascend and resume the search
  1761. */
  1762. if (this_parent != parent) {
  1763. next = this_parent->mnt_child.next;
  1764. this_parent = this_parent->mnt_parent;
  1765. goto resume;
  1766. }
  1767. return found;
  1768. }
  1769. /*
  1770. * process a list of expirable mountpoints with the intent of discarding any
  1771. * submounts of a specific parent mountpoint
  1772. *
  1773. * vfsmount_lock must be held for write
  1774. */
  1775. static void shrink_submounts(struct mount *mnt, struct list_head *umounts)
  1776. {
  1777. LIST_HEAD(graveyard);
  1778. struct mount *m;
  1779. /* extract submounts of 'mountpoint' from the expiration list */
  1780. while (select_submounts(mnt, &graveyard)) {
  1781. while (!list_empty(&graveyard)) {
  1782. m = list_first_entry(&graveyard, struct mount,
  1783. mnt_expire);
  1784. touch_mnt_namespace(m->mnt_ns);
  1785. umount_tree(m, 1, umounts);
  1786. }
  1787. }
  1788. }
  1789. /*
  1790. * Some copy_from_user() implementations do not return the exact number of
  1791. * bytes remaining to copy on a fault. But copy_mount_options() requires that.
  1792. * Note that this function differs from copy_from_user() in that it will oops
  1793. * on bad values of `to', rather than returning a short copy.
  1794. */
  1795. static long exact_copy_from_user(void *to, const void __user * from,
  1796. unsigned long n)
  1797. {
  1798. char *t = to;
  1799. const char __user *f = from;
  1800. char c;
  1801. if (!access_ok(VERIFY_READ, from, n))
  1802. return n;
  1803. while (n) {
  1804. if (__get_user(c, f)) {
  1805. memset(t, 0, n);
  1806. break;
  1807. }
  1808. *t++ = c;
  1809. f++;
  1810. n--;
  1811. }
  1812. return n;
  1813. }
  1814. int copy_mount_options(const void __user * data, unsigned long *where)
  1815. {
  1816. int i;
  1817. unsigned long page;
  1818. unsigned long size;
  1819. *where = 0;
  1820. if (!data)
  1821. return 0;
  1822. if (!(page = __get_free_page(GFP_KERNEL)))
  1823. return -ENOMEM;
  1824. /* We only care that *some* data at the address the user
  1825. * gave us is valid. Just in case, we'll zero
  1826. * the remainder of the page.
  1827. */
  1828. /* copy_from_user cannot cross TASK_SIZE ! */
  1829. size = TASK_SIZE - (unsigned long)data;
  1830. if (size > PAGE_SIZE)
  1831. size = PAGE_SIZE;
  1832. i = size - exact_copy_from_user((void *)page, data, size);
  1833. if (!i) {
  1834. free_page(page);
  1835. return -EFAULT;
  1836. }
  1837. if (i != PAGE_SIZE)
  1838. memset((char *)page + i, 0, PAGE_SIZE - i);
  1839. *where = page;
  1840. return 0;
  1841. }
  1842. int copy_mount_string(const void __user *data, char **where)
  1843. {
  1844. char *tmp;
  1845. if (!data) {
  1846. *where = NULL;
  1847. return 0;
  1848. }
  1849. tmp = strndup_user(data, PAGE_SIZE);
  1850. if (IS_ERR(tmp))
  1851. return PTR_ERR(tmp);
  1852. *where = tmp;
  1853. return 0;
  1854. }
  1855. /*
  1856. * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
  1857. * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
  1858. *
  1859. * data is a (void *) that can point to any structure up to
  1860. * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
  1861. * information (or be NULL).
  1862. *
  1863. * Pre-0.97 versions of mount() didn't have a flags word.
  1864. * When the flags word was introduced its top half was required
  1865. * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
  1866. * Therefore, if this magic number is present, it carries no information
  1867. * and must be discarded.
  1868. */
  1869. long do_mount(char *dev_name, char *dir_name, char *type_page,
  1870. unsigned long flags, void *data_page)
  1871. {
  1872. struct path path;
  1873. int retval = 0;
  1874. int mnt_flags = 0;
  1875. /* Discard magic */
  1876. if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
  1877. flags &= ~MS_MGC_MSK;
  1878. /* Basic sanity checks */
  1879. if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
  1880. return -EINVAL;
  1881. if (data_page)
  1882. ((char *)data_page)[PAGE_SIZE - 1] = 0;
  1883. /* ... and get the mountpoint */
  1884. retval = kern_path(dir_name, LOOKUP_FOLLOW, &path);
  1885. if (retval)
  1886. return retval;
  1887. retval = security_sb_mount(dev_name, &path,
  1888. type_page, flags, data_page);
  1889. if (retval)
  1890. goto dput_out;
  1891. /* Default to relatime unless overriden */
  1892. if (!(flags & MS_NOATIME))
  1893. mnt_flags |= MNT_RELATIME;
  1894. /* Separate the per-mountpoint flags */
  1895. if (flags & MS_NOSUID)
  1896. mnt_flags |= MNT_NOSUID;
  1897. if (flags & MS_NODEV)
  1898. mnt_flags |= MNT_NODEV;
  1899. if (flags & MS_NOEXEC)
  1900. mnt_flags |= MNT_NOEXEC;
  1901. if (flags & MS_NOATIME)
  1902. mnt_flags |= MNT_NOATIME;
  1903. if (flags & MS_NODIRATIME)
  1904. mnt_flags |= MNT_NODIRATIME;
  1905. if (flags & MS_STRICTATIME)
  1906. mnt_flags &= ~(MNT_RELATIME | MNT_NOATIME);
  1907. if (flags & MS_RDONLY)
  1908. mnt_flags |= MNT_READONLY;
  1909. flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE | MS_BORN |
  1910. MS_NOATIME | MS_NODIRATIME | MS_RELATIME| MS_KERNMOUNT |
  1911. MS_STRICTATIME);
  1912. if (flags & MS_REMOUNT)
  1913. retval = do_remount(&path, flags & ~MS_REMOUNT, mnt_flags,
  1914. data_page);
  1915. else if (flags & MS_BIND)
  1916. retval = do_loopback(&path, dev_name, flags & MS_REC);
  1917. else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
  1918. retval = do_change_type(&path, flags);
  1919. else if (flags & MS_MOVE)
  1920. retval = do_move_mount(&path, dev_name);
  1921. else
  1922. retval = do_new_mount(&path, type_page, flags, mnt_flags,
  1923. dev_name, data_page);
  1924. dput_out:
  1925. path_put(&path);
  1926. return retval;
  1927. }
  1928. static struct mnt_namespace *alloc_mnt_ns(void)
  1929. {
  1930. struct mnt_namespace *new_ns;
  1931. new_ns = kmalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
  1932. if (!new_ns)
  1933. return ERR_PTR(-ENOMEM);
  1934. atomic_set(&new_ns->count, 1);
  1935. new_ns->root = NULL;
  1936. INIT_LIST_HEAD(&new_ns->list);
  1937. init_waitqueue_head(&new_ns->poll);
  1938. new_ns->event = 0;
  1939. return new_ns;
  1940. }
  1941. void mnt_make_longterm(struct vfsmount *mnt)
  1942. {
  1943. __mnt_make_longterm(real_mount(mnt));
  1944. }
  1945. void mnt_make_shortterm(struct vfsmount *m)
  1946. {
  1947. #ifdef CONFIG_SMP
  1948. struct mount *mnt = real_mount(m);
  1949. if (atomic_add_unless(&mnt->mnt_longterm, -1, 1))
  1950. return;
  1951. br_write_lock(vfsmount_lock);
  1952. atomic_dec(&mnt->mnt_longterm);
  1953. br_write_unlock(vfsmount_lock);
  1954. #endif
  1955. }
  1956. /*
  1957. * Allocate a new namespace structure and populate it with contents
  1958. * copied from the namespace of the passed in task structure.
  1959. */
  1960. static struct mnt_namespace *dup_mnt_ns(struct mnt_namespace *mnt_ns,
  1961. struct fs_struct *fs)
  1962. {
  1963. struct mnt_namespace *new_ns;
  1964. struct vfsmount *rootmnt = NULL, *pwdmnt = NULL;
  1965. struct mount *p, *q;
  1966. struct mount *old = mnt_ns->root;
  1967. struct mount *new;
  1968. new_ns = alloc_mnt_ns();
  1969. if (IS_ERR(new_ns))
  1970. return new_ns;
  1971. down_write(&namespace_sem);
  1972. /* First pass: copy the tree topology */
  1973. new = copy_tree(old, old->mnt.mnt_root, CL_COPY_ALL | CL_EXPIRE);
  1974. if (!new) {
  1975. up_write(&namespace_sem);
  1976. kfree(new_ns);
  1977. return ERR_PTR(-ENOMEM);
  1978. }
  1979. new_ns->root = new;
  1980. br_write_lock(vfsmount_lock);
  1981. list_add_tail(&new_ns->list, &new->mnt_list);
  1982. br_write_unlock(vfsmount_lock);
  1983. /*
  1984. * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
  1985. * as belonging to new namespace. We have already acquired a private
  1986. * fs_struct, so tsk->fs->lock is not needed.
  1987. */
  1988. p = old;
  1989. q = new;
  1990. while (p) {
  1991. q->mnt_ns = new_ns;
  1992. __mnt_make_longterm(q);
  1993. if (fs) {
  1994. if (&p->mnt == fs->root.mnt) {
  1995. fs->root.mnt = mntget(&q->mnt);
  1996. __mnt_make_longterm(q);
  1997. mnt_make_shortterm(&p->mnt);
  1998. rootmnt = &p->mnt;
  1999. }
  2000. if (&p->mnt == fs->pwd.mnt) {
  2001. fs->pwd.mnt = mntget(&q->mnt);
  2002. __mnt_make_longterm(q);
  2003. mnt_make_shortterm(&p->mnt);
  2004. pwdmnt = &p->mnt;
  2005. }
  2006. }
  2007. p = next_mnt(p, old);
  2008. q = next_mnt(q, new);
  2009. }
  2010. up_write(&namespace_sem);
  2011. if (rootmnt)
  2012. mntput(rootmnt);
  2013. if (pwdmnt)
  2014. mntput(pwdmnt);
  2015. return new_ns;
  2016. }
  2017. struct mnt_namespace *copy_mnt_ns(unsigned long flags, struct mnt_namespace *ns,
  2018. struct fs_struct *new_fs)
  2019. {
  2020. struct mnt_namespace *new_ns;
  2021. BUG_ON(!ns);
  2022. get_mnt_ns(ns);
  2023. if (!(flags & CLONE_NEWNS))
  2024. return ns;
  2025. new_ns = dup_mnt_ns(ns, new_fs);
  2026. put_mnt_ns(ns);
  2027. return new_ns;
  2028. }
  2029. /**
  2030. * create_mnt_ns - creates a private namespace and adds a root filesystem
  2031. * @mnt: pointer to the new root filesystem mountpoint
  2032. */
  2033. static struct mnt_namespace *create_mnt_ns(struct vfsmount *m)
  2034. {
  2035. struct mnt_namespace *new_ns = alloc_mnt_ns();
  2036. if (!IS_ERR(new_ns)) {
  2037. struct mount *mnt = real_mount(m);
  2038. mnt->mnt_ns = new_ns;
  2039. __mnt_make_longterm(mnt);
  2040. new_ns->root = mnt;
  2041. list_add(&new_ns->list, &mnt->mnt_list);
  2042. } else {
  2043. mntput(m);
  2044. }
  2045. return new_ns;
  2046. }
  2047. struct dentry *mount_subtree(struct vfsmount *mnt, const char *name)
  2048. {
  2049. struct mnt_namespace *ns;
  2050. struct super_block *s;
  2051. struct path path;
  2052. int err;
  2053. ns = create_mnt_ns(mnt);
  2054. if (IS_ERR(ns))
  2055. return ERR_CAST(ns);
  2056. err = vfs_path_lookup(mnt->mnt_root, mnt,
  2057. name, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
  2058. put_mnt_ns(ns);
  2059. if (err)
  2060. return ERR_PTR(err);
  2061. /* trade a vfsmount reference for active sb one */
  2062. s = path.mnt->mnt_sb;
  2063. atomic_inc(&s->s_active);
  2064. mntput(path.mnt);
  2065. /* lock the sucker */
  2066. down_write(&s->s_umount);
  2067. /* ... and return the root of (sub)tree on it */
  2068. return path.dentry;
  2069. }
  2070. EXPORT_SYMBOL(mount_subtree);
  2071. SYSCALL_DEFINE5(mount, char __user *, dev_name, char __user *, dir_name,
  2072. char __user *, type, unsigned long, flags, void __user *, data)
  2073. {
  2074. int ret;
  2075. char *kernel_type;
  2076. char *kernel_dir;
  2077. char *kernel_dev;
  2078. unsigned long data_page;
  2079. ret = copy_mount_string(type, &kernel_type);
  2080. if (ret < 0)
  2081. goto out_type;
  2082. kernel_dir = getname(dir_name);
  2083. if (IS_ERR(kernel_dir)) {
  2084. ret = PTR_ERR(kernel_dir);
  2085. goto out_dir;
  2086. }
  2087. ret = copy_mount_string(dev_name, &kernel_dev);
  2088. if (ret < 0)
  2089. goto out_dev;
  2090. ret = copy_mount_options(data, &data_page);
  2091. if (ret < 0)
  2092. goto out_data;
  2093. ret = do_mount(kernel_dev, kernel_dir, kernel_type, flags,
  2094. (void *) data_page);
  2095. free_page(data_page);
  2096. out_data:
  2097. kfree(kernel_dev);
  2098. out_dev:
  2099. putname(kernel_dir);
  2100. out_dir:
  2101. kfree(kernel_type);
  2102. out_type:
  2103. return ret;
  2104. }
  2105. /*
  2106. * Return true if path is reachable from root
  2107. *
  2108. * namespace_sem or vfsmount_lock is held
  2109. */
  2110. bool is_path_reachable(struct mount *mnt, struct dentry *dentry,
  2111. const struct path *root)
  2112. {
  2113. while (&mnt->mnt != root->mnt && mnt_has_parent(mnt)) {
  2114. dentry = mnt->mnt_mountpoint;
  2115. mnt = mnt->mnt_parent;
  2116. }
  2117. return &mnt->mnt == root->mnt && is_subdir(dentry, root->dentry);
  2118. }
  2119. int path_is_under(struct path *path1, struct path *path2)
  2120. {
  2121. int res;
  2122. br_read_lock(vfsmount_lock);
  2123. res = is_path_reachable(real_mount(path1->mnt), path1->dentry, path2);
  2124. br_read_unlock(vfsmount_lock);
  2125. return res;
  2126. }
  2127. EXPORT_SYMBOL(path_is_under);
  2128. /*
  2129. * pivot_root Semantics:
  2130. * Moves the root file system of the current process to the directory put_old,
  2131. * makes new_root as the new root file system of the current process, and sets
  2132. * root/cwd of all processes which had them on the current root to new_root.
  2133. *
  2134. * Restrictions:
  2135. * The new_root and put_old must be directories, and must not be on the
  2136. * same file system as the current process root. The put_old must be
  2137. * underneath new_root, i.e. adding a non-zero number of /.. to the string
  2138. * pointed to by put_old must yield the same directory as new_root. No other
  2139. * file system may be mounted on put_old. After all, new_root is a mountpoint.
  2140. *
  2141. * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
  2142. * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
  2143. * in this situation.
  2144. *
  2145. * Notes:
  2146. * - we don't move root/cwd if they are not at the root (reason: if something
  2147. * cared enough to change them, it's probably wrong to force them elsewhere)
  2148. * - it's okay to pick a root that isn't the root of a file system, e.g.
  2149. * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
  2150. * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
  2151. * first.
  2152. */
  2153. SYSCALL_DEFINE2(pivot_root, const char __user *, new_root,
  2154. const char __user *, put_old)
  2155. {
  2156. struct path new, old, parent_path, root_parent, root;
  2157. struct mount *new_mnt, *root_mnt;
  2158. int error;
  2159. if (!capable(CAP_SYS_ADMIN))
  2160. return -EPERM;
  2161. error = user_path_dir(new_root, &new);
  2162. if (error)
  2163. goto out0;
  2164. error = user_path_dir(put_old, &old);
  2165. if (error)
  2166. goto out1;
  2167. error = security_sb_pivotroot(&old, &new);
  2168. if (error)
  2169. goto out2;
  2170. get_fs_root(current->fs, &root);
  2171. error = lock_mount(&old);
  2172. if (error)
  2173. goto out3;
  2174. error = -EINVAL;
  2175. new_mnt = real_mount(new.mnt);
  2176. root_mnt = real_mount(root.mnt);
  2177. if (IS_MNT_SHARED(real_mount(old.mnt)) ||
  2178. IS_MNT_SHARED(new_mnt->mnt_parent) ||
  2179. IS_MNT_SHARED(root_mnt->mnt_parent))
  2180. goto out4;
  2181. if (!check_mnt(root_mnt) || !check_mnt(new_mnt))
  2182. goto out4;
  2183. error = -ENOENT;
  2184. if (d_unlinked(new.dentry))
  2185. goto out4;
  2186. if (d_unlinked(old.dentry))
  2187. goto out4;
  2188. error = -EBUSY;
  2189. if (new.mnt == root.mnt ||
  2190. old.mnt == root.mnt)
  2191. goto out4; /* loop, on the same file system */
  2192. error = -EINVAL;
  2193. if (root.mnt->mnt_root != root.dentry)
  2194. goto out4; /* not a mountpoint */
  2195. if (!mnt_has_parent(root_mnt))
  2196. goto out4; /* not attached */
  2197. if (new.mnt->mnt_root != new.dentry)
  2198. goto out4; /* not a mountpoint */
  2199. if (!mnt_has_parent(new_mnt))
  2200. goto out4; /* not attached */
  2201. /* make sure we can reach put_old from new_root */
  2202. if (!is_path_reachable(real_mount(old.mnt), old.dentry, &new))
  2203. goto out4;
  2204. br_write_lock(vfsmount_lock);
  2205. detach_mnt(new_mnt, &parent_path);
  2206. detach_mnt(root_mnt, &root_parent);
  2207. /* mount old root on put_old */
  2208. attach_mnt(root_mnt, &old);
  2209. /* mount new_root on / */
  2210. attach_mnt(new_mnt, &root_parent);
  2211. touch_mnt_namespace(current->nsproxy->mnt_ns);
  2212. br_write_unlock(vfsmount_lock);
  2213. chroot_fs_refs(&root, &new);
  2214. error = 0;
  2215. out4:
  2216. unlock_mount(&old);
  2217. if (!error) {
  2218. path_put(&root_parent);
  2219. path_put(&parent_path);
  2220. }
  2221. out3:
  2222. path_put(&root);
  2223. out2:
  2224. path_put(&old);
  2225. out1:
  2226. path_put(&new);
  2227. out0:
  2228. return error;
  2229. }
  2230. static void __init init_mount_tree(void)
  2231. {
  2232. struct vfsmount *mnt;
  2233. struct mnt_namespace *ns;
  2234. struct path root;
  2235. mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  2236. if (IS_ERR(mnt))
  2237. panic("Can't create rootfs");
  2238. ns = create_mnt_ns(mnt);
  2239. if (IS_ERR(ns))
  2240. panic("Can't allocate initial namespace");
  2241. init_task.nsproxy->mnt_ns = ns;
  2242. get_mnt_ns(ns);
  2243. root.mnt = mnt;
  2244. root.dentry = mnt->mnt_root;
  2245. set_fs_pwd(current->fs, &root);
  2246. set_fs_root(current->fs, &root);
  2247. }
  2248. void __init mnt_init(void)
  2249. {
  2250. unsigned u;
  2251. int err;
  2252. init_rwsem(&namespace_sem);
  2253. mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
  2254. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2255. mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
  2256. if (!mount_hashtable)
  2257. panic("Failed to allocate mount hash table\n");
  2258. printk(KERN_INFO "Mount-cache hash table entries: %lu\n", HASH_SIZE);
  2259. for (u = 0; u < HASH_SIZE; u++)
  2260. INIT_LIST_HEAD(&mount_hashtable[u]);
  2261. br_lock_init(vfsmount_lock);
  2262. err = sysfs_init();
  2263. if (err)
  2264. printk(KERN_WARNING "%s: sysfs_init error: %d\n",
  2265. __func__, err);
  2266. fs_kobj = kobject_create_and_add("fs", NULL);
  2267. if (!fs_kobj)
  2268. printk(KERN_WARNING "%s: kobj create error\n", __func__);
  2269. init_rootfs();
  2270. init_mount_tree();
  2271. }
  2272. void put_mnt_ns(struct mnt_namespace *ns)
  2273. {
  2274. LIST_HEAD(umount_list);
  2275. if (!atomic_dec_and_test(&ns->count))
  2276. return;
  2277. down_write(&namespace_sem);
  2278. br_write_lock(vfsmount_lock);
  2279. umount_tree(ns->root, 0, &umount_list);
  2280. br_write_unlock(vfsmount_lock);
  2281. up_write(&namespace_sem);
  2282. release_mounts(&umount_list);
  2283. kfree(ns);
  2284. }
  2285. struct vfsmount *kern_mount_data(struct file_system_type *type, void *data)
  2286. {
  2287. struct vfsmount *mnt;
  2288. mnt = vfs_kern_mount(type, MS_KERNMOUNT, type->name, data);
  2289. if (!IS_ERR(mnt)) {
  2290. /*
  2291. * it is a longterm mount, don't release mnt until
  2292. * we unmount before file sys is unregistered
  2293. */
  2294. mnt_make_longterm(mnt);
  2295. }
  2296. return mnt;
  2297. }
  2298. EXPORT_SYMBOL_GPL(kern_mount_data);
  2299. void kern_unmount(struct vfsmount *mnt)
  2300. {
  2301. /* release long term mount so mount point can be released */
  2302. if (!IS_ERR_OR_NULL(mnt)) {
  2303. mnt_make_shortterm(mnt);
  2304. mntput(mnt);
  2305. }
  2306. }
  2307. EXPORT_SYMBOL(kern_unmount);
  2308. bool our_mnt(struct vfsmount *mnt)
  2309. {
  2310. return check_mnt(real_mount(mnt));
  2311. }