namespace.c 62 KB

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