shmem.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718
  1. /*
  2. * Resizable virtual memory filesystem for Linux.
  3. *
  4. * Copyright (C) 2000 Linus Torvalds.
  5. * 2000 Transmeta Corp.
  6. * 2000-2001 Christoph Rohland
  7. * 2000-2001 SAP AG
  8. * 2002 Red Hat Inc.
  9. * Copyright (C) 2002-2005 Hugh Dickins.
  10. * Copyright (C) 2002-2005 VERITAS Software Corporation.
  11. * Copyright (C) 2004 Andi Kleen, SuSE Labs
  12. *
  13. * Extended attribute support for tmpfs:
  14. * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  15. * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
  16. *
  17. * tiny-shmem:
  18. * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
  19. *
  20. * This file is released under the GPL.
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <linux/vfs.h>
  25. #include <linux/mount.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/file.h>
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/swap.h>
  31. static struct vfsmount *shm_mnt;
  32. #ifdef CONFIG_SHMEM
  33. /*
  34. * This virtual memory filesystem is heavily based on the ramfs. It
  35. * extends ramfs by the ability to use swap and honor resource limits
  36. * which makes it a completely usable filesystem.
  37. */
  38. #include <linux/xattr.h>
  39. #include <linux/exportfs.h>
  40. #include <linux/posix_acl.h>
  41. #include <linux/generic_acl.h>
  42. #include <linux/mman.h>
  43. #include <linux/string.h>
  44. #include <linux/slab.h>
  45. #include <linux/backing-dev.h>
  46. #include <linux/shmem_fs.h>
  47. #include <linux/writeback.h>
  48. #include <linux/blkdev.h>
  49. #include <linux/pagevec.h>
  50. #include <linux/percpu_counter.h>
  51. #include <linux/splice.h>
  52. #include <linux/security.h>
  53. #include <linux/swapops.h>
  54. #include <linux/mempolicy.h>
  55. #include <linux/namei.h>
  56. #include <linux/ctype.h>
  57. #include <linux/migrate.h>
  58. #include <linux/highmem.h>
  59. #include <linux/seq_file.h>
  60. #include <linux/magic.h>
  61. #include <asm/uaccess.h>
  62. #include <asm/pgtable.h>
  63. #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512)
  64. #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)
  65. /* Pretend that each entry is of this size in directory's i_size */
  66. #define BOGO_DIRENT_SIZE 20
  67. struct shmem_xattr {
  68. struct list_head list; /* anchored by shmem_inode_info->xattr_list */
  69. char *name; /* xattr name */
  70. size_t size;
  71. char value[0];
  72. };
  73. /* Flag allocation requirements to shmem_getpage */
  74. enum sgp_type {
  75. SGP_READ, /* don't exceed i_size, don't allocate page */
  76. SGP_CACHE, /* don't exceed i_size, may allocate page */
  77. SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */
  78. SGP_WRITE, /* may exceed i_size, may allocate page */
  79. };
  80. #ifdef CONFIG_TMPFS
  81. static unsigned long shmem_default_max_blocks(void)
  82. {
  83. return totalram_pages / 2;
  84. }
  85. static unsigned long shmem_default_max_inodes(void)
  86. {
  87. return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
  88. }
  89. #endif
  90. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  91. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type);
  92. static inline int shmem_getpage(struct inode *inode, pgoff_t index,
  93. struct page **pagep, enum sgp_type sgp, int *fault_type)
  94. {
  95. return shmem_getpage_gfp(inode, index, pagep, sgp,
  96. mapping_gfp_mask(inode->i_mapping), fault_type);
  97. }
  98. static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
  99. {
  100. return sb->s_fs_info;
  101. }
  102. /*
  103. * shmem_file_setup pre-accounts the whole fixed size of a VM object,
  104. * for shared memory and for shared anonymous (/dev/zero) mappings
  105. * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
  106. * consistent with the pre-accounting of private mappings ...
  107. */
  108. static inline int shmem_acct_size(unsigned long flags, loff_t size)
  109. {
  110. return (flags & VM_NORESERVE) ?
  111. 0 : security_vm_enough_memory_kern(VM_ACCT(size));
  112. }
  113. static inline void shmem_unacct_size(unsigned long flags, loff_t size)
  114. {
  115. if (!(flags & VM_NORESERVE))
  116. vm_unacct_memory(VM_ACCT(size));
  117. }
  118. /*
  119. * ... whereas tmpfs objects are accounted incrementally as
  120. * pages are allocated, in order to allow huge sparse files.
  121. * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
  122. * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
  123. */
  124. static inline int shmem_acct_block(unsigned long flags)
  125. {
  126. return (flags & VM_NORESERVE) ?
  127. security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
  128. }
  129. static inline void shmem_unacct_blocks(unsigned long flags, long pages)
  130. {
  131. if (flags & VM_NORESERVE)
  132. vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
  133. }
  134. static const struct super_operations shmem_ops;
  135. static const struct address_space_operations shmem_aops;
  136. static const struct file_operations shmem_file_operations;
  137. static const struct inode_operations shmem_inode_operations;
  138. static const struct inode_operations shmem_dir_inode_operations;
  139. static const struct inode_operations shmem_special_inode_operations;
  140. static const struct vm_operations_struct shmem_vm_ops;
  141. static struct backing_dev_info shmem_backing_dev_info __read_mostly = {
  142. .ra_pages = 0, /* No readahead */
  143. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED,
  144. };
  145. static LIST_HEAD(shmem_swaplist);
  146. static DEFINE_MUTEX(shmem_swaplist_mutex);
  147. static void shmem_free_blocks(struct inode *inode, long pages)
  148. {
  149. struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
  150. if (sbinfo->max_blocks) {
  151. percpu_counter_add(&sbinfo->used_blocks, -pages);
  152. inode->i_blocks -= pages*BLOCKS_PER_PAGE;
  153. }
  154. }
  155. static int shmem_reserve_inode(struct super_block *sb)
  156. {
  157. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  158. if (sbinfo->max_inodes) {
  159. spin_lock(&sbinfo->stat_lock);
  160. if (!sbinfo->free_inodes) {
  161. spin_unlock(&sbinfo->stat_lock);
  162. return -ENOSPC;
  163. }
  164. sbinfo->free_inodes--;
  165. spin_unlock(&sbinfo->stat_lock);
  166. }
  167. return 0;
  168. }
  169. static void shmem_free_inode(struct super_block *sb)
  170. {
  171. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  172. if (sbinfo->max_inodes) {
  173. spin_lock(&sbinfo->stat_lock);
  174. sbinfo->free_inodes++;
  175. spin_unlock(&sbinfo->stat_lock);
  176. }
  177. }
  178. /**
  179. * shmem_recalc_inode - recalculate the block usage of an inode
  180. * @inode: inode to recalc
  181. *
  182. * We have to calculate the free blocks since the mm can drop
  183. * undirtied hole pages behind our back.
  184. *
  185. * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
  186. * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
  187. *
  188. * It has to be called with the spinlock held.
  189. */
  190. static void shmem_recalc_inode(struct inode *inode)
  191. {
  192. struct shmem_inode_info *info = SHMEM_I(inode);
  193. long freed;
  194. freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
  195. if (freed > 0) {
  196. info->alloced -= freed;
  197. shmem_unacct_blocks(info->flags, freed);
  198. shmem_free_blocks(inode, freed);
  199. }
  200. }
  201. static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index,
  202. swp_entry_t swap)
  203. {
  204. if (index < SHMEM_NR_DIRECT)
  205. info->i_direct[index] = swap;
  206. }
  207. static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index)
  208. {
  209. return (index < SHMEM_NR_DIRECT) ?
  210. info->i_direct[index] : (swp_entry_t){0};
  211. }
  212. /*
  213. * Replace item expected in radix tree by a new item, while holding tree lock.
  214. */
  215. static int shmem_radix_tree_replace(struct address_space *mapping,
  216. pgoff_t index, void *expected, void *replacement)
  217. {
  218. void **pslot;
  219. void *item = NULL;
  220. VM_BUG_ON(!expected);
  221. pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
  222. if (pslot)
  223. item = radix_tree_deref_slot_protected(pslot,
  224. &mapping->tree_lock);
  225. if (item != expected)
  226. return -ENOENT;
  227. if (replacement)
  228. radix_tree_replace_slot(pslot, replacement);
  229. else
  230. radix_tree_delete(&mapping->page_tree, index);
  231. return 0;
  232. }
  233. /*
  234. * Like add_to_page_cache_locked, but error if expected item has gone.
  235. */
  236. static int shmem_add_to_page_cache(struct page *page,
  237. struct address_space *mapping,
  238. pgoff_t index, gfp_t gfp, void *expected)
  239. {
  240. int error;
  241. VM_BUG_ON(!PageLocked(page));
  242. VM_BUG_ON(!PageSwapBacked(page));
  243. error = mem_cgroup_cache_charge(page, current->mm,
  244. gfp & GFP_RECLAIM_MASK);
  245. if (error)
  246. goto out;
  247. if (!expected)
  248. error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
  249. if (!error) {
  250. page_cache_get(page);
  251. page->mapping = mapping;
  252. page->index = index;
  253. spin_lock_irq(&mapping->tree_lock);
  254. if (!expected)
  255. error = radix_tree_insert(&mapping->page_tree,
  256. index, page);
  257. else
  258. error = shmem_radix_tree_replace(mapping, index,
  259. expected, page);
  260. if (!error) {
  261. mapping->nrpages++;
  262. __inc_zone_page_state(page, NR_FILE_PAGES);
  263. __inc_zone_page_state(page, NR_SHMEM);
  264. spin_unlock_irq(&mapping->tree_lock);
  265. } else {
  266. page->mapping = NULL;
  267. spin_unlock_irq(&mapping->tree_lock);
  268. page_cache_release(page);
  269. }
  270. if (!expected)
  271. radix_tree_preload_end();
  272. }
  273. if (error)
  274. mem_cgroup_uncharge_cache_page(page);
  275. out:
  276. return error;
  277. }
  278. /*
  279. * Like find_get_pages, but collecting swap entries as well as pages.
  280. */
  281. static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
  282. pgoff_t start, unsigned int nr_pages,
  283. struct page **pages, pgoff_t *indices)
  284. {
  285. unsigned int i;
  286. unsigned int ret;
  287. unsigned int nr_found;
  288. rcu_read_lock();
  289. restart:
  290. nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
  291. (void ***)pages, indices, start, nr_pages);
  292. ret = 0;
  293. for (i = 0; i < nr_found; i++) {
  294. struct page *page;
  295. repeat:
  296. page = radix_tree_deref_slot((void **)pages[i]);
  297. if (unlikely(!page))
  298. continue;
  299. if (radix_tree_exception(page)) {
  300. if (radix_tree_exceptional_entry(page))
  301. goto export;
  302. /* radix_tree_deref_retry(page) */
  303. goto restart;
  304. }
  305. if (!page_cache_get_speculative(page))
  306. goto repeat;
  307. /* Has the page moved? */
  308. if (unlikely(page != *((void **)pages[i]))) {
  309. page_cache_release(page);
  310. goto repeat;
  311. }
  312. export:
  313. indices[ret] = indices[i];
  314. pages[ret] = page;
  315. ret++;
  316. }
  317. if (unlikely(!ret && nr_found))
  318. goto restart;
  319. rcu_read_unlock();
  320. return ret;
  321. }
  322. /*
  323. * Lockless lookup of swap entry in radix tree, avoiding refcount on pages.
  324. */
  325. static pgoff_t shmem_find_swap(struct address_space *mapping, void *radswap)
  326. {
  327. void **slots[PAGEVEC_SIZE];
  328. pgoff_t indices[PAGEVEC_SIZE];
  329. unsigned int nr_found;
  330. restart:
  331. nr_found = 1;
  332. indices[0] = -1;
  333. while (nr_found) {
  334. pgoff_t index = indices[nr_found - 1] + 1;
  335. unsigned int i;
  336. rcu_read_lock();
  337. nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree,
  338. slots, indices, index, PAGEVEC_SIZE);
  339. for (i = 0; i < nr_found; i++) {
  340. void *item = radix_tree_deref_slot(slots[i]);
  341. if (radix_tree_deref_retry(item)) {
  342. rcu_read_unlock();
  343. goto restart;
  344. }
  345. if (item == radswap) {
  346. rcu_read_unlock();
  347. return indices[i];
  348. }
  349. }
  350. rcu_read_unlock();
  351. cond_resched();
  352. }
  353. return -1;
  354. }
  355. /*
  356. * Remove swap entry from radix tree, free the swap and its page cache.
  357. */
  358. static int shmem_free_swap(struct address_space *mapping,
  359. pgoff_t index, void *radswap)
  360. {
  361. int error;
  362. spin_lock_irq(&mapping->tree_lock);
  363. error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
  364. spin_unlock_irq(&mapping->tree_lock);
  365. if (!error)
  366. free_swap_and_cache(radix_to_swp_entry(radswap));
  367. return error;
  368. }
  369. /*
  370. * Pagevec may contain swap entries, so shuffle up pages before releasing.
  371. */
  372. static void shmem_pagevec_release(struct pagevec *pvec)
  373. {
  374. int i, j;
  375. for (i = 0, j = 0; i < pagevec_count(pvec); i++) {
  376. struct page *page = pvec->pages[i];
  377. if (!radix_tree_exceptional_entry(page))
  378. pvec->pages[j++] = page;
  379. }
  380. pvec->nr = j;
  381. pagevec_release(pvec);
  382. }
  383. /*
  384. * Remove range of pages and swap entries from radix tree, and free them.
  385. */
  386. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  387. {
  388. struct address_space *mapping = inode->i_mapping;
  389. struct shmem_inode_info *info = SHMEM_I(inode);
  390. pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  391. unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  392. pgoff_t end = (lend >> PAGE_CACHE_SHIFT);
  393. struct pagevec pvec;
  394. pgoff_t indices[PAGEVEC_SIZE];
  395. long nr_swaps_freed = 0;
  396. pgoff_t index;
  397. int i;
  398. BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
  399. pagevec_init(&pvec, 0);
  400. index = start;
  401. while (index <= end) {
  402. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  403. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  404. pvec.pages, indices);
  405. if (!pvec.nr)
  406. break;
  407. mem_cgroup_uncharge_start();
  408. for (i = 0; i < pagevec_count(&pvec); i++) {
  409. struct page *page = pvec.pages[i];
  410. index = indices[i];
  411. if (index > end)
  412. break;
  413. if (radix_tree_exceptional_entry(page)) {
  414. nr_swaps_freed += !shmem_free_swap(mapping,
  415. index, page);
  416. continue;
  417. }
  418. if (!trylock_page(page))
  419. continue;
  420. if (page->mapping == mapping) {
  421. VM_BUG_ON(PageWriteback(page));
  422. truncate_inode_page(mapping, page);
  423. }
  424. unlock_page(page);
  425. }
  426. shmem_pagevec_release(&pvec);
  427. mem_cgroup_uncharge_end();
  428. cond_resched();
  429. index++;
  430. }
  431. if (partial) {
  432. struct page *page = NULL;
  433. shmem_getpage(inode, start - 1, &page, SGP_READ, NULL);
  434. if (page) {
  435. zero_user_segment(page, partial, PAGE_CACHE_SIZE);
  436. set_page_dirty(page);
  437. unlock_page(page);
  438. page_cache_release(page);
  439. }
  440. }
  441. index = start;
  442. for ( ; ; ) {
  443. cond_resched();
  444. pvec.nr = shmem_find_get_pages_and_swap(mapping, index,
  445. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  446. pvec.pages, indices);
  447. if (!pvec.nr) {
  448. if (index == start)
  449. break;
  450. index = start;
  451. continue;
  452. }
  453. if (index == start && indices[0] > end) {
  454. shmem_pagevec_release(&pvec);
  455. break;
  456. }
  457. mem_cgroup_uncharge_start();
  458. for (i = 0; i < pagevec_count(&pvec); i++) {
  459. struct page *page = pvec.pages[i];
  460. index = indices[i];
  461. if (index > end)
  462. break;
  463. if (radix_tree_exceptional_entry(page)) {
  464. nr_swaps_freed += !shmem_free_swap(mapping,
  465. index, page);
  466. continue;
  467. }
  468. lock_page(page);
  469. if (page->mapping == mapping) {
  470. VM_BUG_ON(PageWriteback(page));
  471. truncate_inode_page(mapping, page);
  472. }
  473. unlock_page(page);
  474. }
  475. shmem_pagevec_release(&pvec);
  476. mem_cgroup_uncharge_end();
  477. index++;
  478. }
  479. spin_lock(&info->lock);
  480. info->swapped -= nr_swaps_freed;
  481. shmem_recalc_inode(inode);
  482. spin_unlock(&info->lock);
  483. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  484. }
  485. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  486. static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
  487. {
  488. struct inode *inode = dentry->d_inode;
  489. int error;
  490. error = inode_change_ok(inode, attr);
  491. if (error)
  492. return error;
  493. if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
  494. loff_t oldsize = inode->i_size;
  495. loff_t newsize = attr->ia_size;
  496. if (newsize != oldsize) {
  497. i_size_write(inode, newsize);
  498. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  499. }
  500. if (newsize < oldsize) {
  501. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  502. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  503. shmem_truncate_range(inode, newsize, (loff_t)-1);
  504. /* unmap again to remove racily COWed private pages */
  505. unmap_mapping_range(inode->i_mapping, holebegin, 0, 1);
  506. }
  507. }
  508. setattr_copy(inode, attr);
  509. #ifdef CONFIG_TMPFS_POSIX_ACL
  510. if (attr->ia_valid & ATTR_MODE)
  511. error = generic_acl_chmod(inode);
  512. #endif
  513. return error;
  514. }
  515. static void shmem_evict_inode(struct inode *inode)
  516. {
  517. struct shmem_inode_info *info = SHMEM_I(inode);
  518. struct shmem_xattr *xattr, *nxattr;
  519. if (inode->i_mapping->a_ops == &shmem_aops) {
  520. shmem_unacct_size(info->flags, inode->i_size);
  521. inode->i_size = 0;
  522. shmem_truncate_range(inode, 0, (loff_t)-1);
  523. if (!list_empty(&info->swaplist)) {
  524. mutex_lock(&shmem_swaplist_mutex);
  525. list_del_init(&info->swaplist);
  526. mutex_unlock(&shmem_swaplist_mutex);
  527. }
  528. }
  529. list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) {
  530. kfree(xattr->name);
  531. kfree(xattr);
  532. }
  533. BUG_ON(inode->i_blocks);
  534. shmem_free_inode(inode->i_sb);
  535. end_writeback(inode);
  536. }
  537. /*
  538. * If swap found in inode, free it and move page from swapcache to filecache.
  539. */
  540. static int shmem_unuse_inode(struct shmem_inode_info *info,
  541. swp_entry_t swap, struct page *page)
  542. {
  543. struct address_space *mapping = info->vfs_inode.i_mapping;
  544. void *radswap;
  545. pgoff_t index;
  546. int error;
  547. radswap = swp_to_radix_entry(swap);
  548. index = shmem_find_swap(mapping, radswap);
  549. if (index == -1)
  550. return 0;
  551. /*
  552. * Move _head_ to start search for next from here.
  553. * But be careful: shmem_evict_inode checks list_empty without taking
  554. * mutex, and there's an instant in list_move_tail when info->swaplist
  555. * would appear empty, if it were the only one on shmem_swaplist.
  556. */
  557. if (shmem_swaplist.next != &info->swaplist)
  558. list_move_tail(&shmem_swaplist, &info->swaplist);
  559. /*
  560. * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
  561. * but also to hold up shmem_evict_inode(): so inode cannot be freed
  562. * beneath us (pagelock doesn't help until the page is in pagecache).
  563. */
  564. error = shmem_add_to_page_cache(page, mapping, index,
  565. GFP_NOWAIT, radswap);
  566. /* which does mem_cgroup_uncharge_cache_page on error */
  567. if (error != -ENOMEM) {
  568. /*
  569. * Truncation and eviction use free_swap_and_cache(), which
  570. * only does trylock page: if we raced, best clean up here.
  571. */
  572. delete_from_swap_cache(page);
  573. set_page_dirty(page);
  574. if (!error) {
  575. spin_lock(&info->lock);
  576. info->swapped--;
  577. spin_unlock(&info->lock);
  578. swap_free(swap);
  579. }
  580. error = 1; /* not an error, but entry was found */
  581. }
  582. return error;
  583. }
  584. /*
  585. * Search through swapped inodes to find and replace swap by page.
  586. */
  587. int shmem_unuse(swp_entry_t swap, struct page *page)
  588. {
  589. struct list_head *this, *next;
  590. struct shmem_inode_info *info;
  591. int found = 0;
  592. int error;
  593. /*
  594. * Charge page using GFP_KERNEL while we can wait, before taking
  595. * the shmem_swaplist_mutex which might hold up shmem_writepage().
  596. * Charged back to the user (not to caller) when swap account is used.
  597. * shmem_add_to_page_cache() will be called with GFP_NOWAIT.
  598. */
  599. error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL);
  600. if (error)
  601. goto out;
  602. /* No radix_tree_preload: swap entry keeps a place for page in tree */
  603. mutex_lock(&shmem_swaplist_mutex);
  604. list_for_each_safe(this, next, &shmem_swaplist) {
  605. info = list_entry(this, struct shmem_inode_info, swaplist);
  606. if (!info->swapped) {
  607. spin_lock(&info->lock);
  608. if (!info->swapped)
  609. list_del_init(&info->swaplist);
  610. spin_unlock(&info->lock);
  611. }
  612. if (info->swapped)
  613. found = shmem_unuse_inode(info, swap, page);
  614. cond_resched();
  615. if (found)
  616. break;
  617. }
  618. mutex_unlock(&shmem_swaplist_mutex);
  619. if (!found)
  620. mem_cgroup_uncharge_cache_page(page);
  621. if (found < 0)
  622. error = found;
  623. out:
  624. unlock_page(page);
  625. page_cache_release(page);
  626. return error;
  627. }
  628. /*
  629. * Move the page from the page cache to the swap cache.
  630. */
  631. static int shmem_writepage(struct page *page, struct writeback_control *wbc)
  632. {
  633. struct shmem_inode_info *info;
  634. swp_entry_t swap, oswap;
  635. struct address_space *mapping;
  636. pgoff_t index;
  637. struct inode *inode;
  638. BUG_ON(!PageLocked(page));
  639. mapping = page->mapping;
  640. index = page->index;
  641. inode = mapping->host;
  642. info = SHMEM_I(inode);
  643. if (info->flags & VM_LOCKED)
  644. goto redirty;
  645. if (!total_swap_pages)
  646. goto redirty;
  647. /*
  648. * shmem_backing_dev_info's capabilities prevent regular writeback or
  649. * sync from ever calling shmem_writepage; but a stacking filesystem
  650. * might use ->writepage of its underlying filesystem, in which case
  651. * tmpfs should write out to swap only in response to memory pressure,
  652. * and not for the writeback threads or sync.
  653. */
  654. if (!wbc->for_reclaim) {
  655. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  656. goto redirty;
  657. }
  658. /*
  659. * Disable even the toy swapping implementation, while we convert
  660. * functions one by one to having swap entries in the radix tree.
  661. */
  662. if (index < ULONG_MAX)
  663. goto redirty;
  664. swap = get_swap_page();
  665. if (!swap.val)
  666. goto redirty;
  667. /*
  668. * Add inode to shmem_unuse()'s list of swapped-out inodes,
  669. * if it's not already there. Do it now because we cannot take
  670. * mutex while holding spinlock, and must do so before the page
  671. * is moved to swap cache, when its pagelock no longer protects
  672. * the inode from eviction. But don't unlock the mutex until
  673. * we've taken the spinlock, because shmem_unuse_inode() will
  674. * prune a !swapped inode from the swaplist under both locks.
  675. */
  676. mutex_lock(&shmem_swaplist_mutex);
  677. if (list_empty(&info->swaplist))
  678. list_add_tail(&info->swaplist, &shmem_swaplist);
  679. spin_lock(&info->lock);
  680. mutex_unlock(&shmem_swaplist_mutex);
  681. oswap = shmem_get_swap(info, index);
  682. if (oswap.val) {
  683. WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
  684. free_swap_and_cache(oswap);
  685. shmem_put_swap(info, index, (swp_entry_t){0});
  686. info->swapped--;
  687. }
  688. shmem_recalc_inode(inode);
  689. if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
  690. delete_from_page_cache(page);
  691. shmem_put_swap(info, index, swap);
  692. info->swapped++;
  693. swap_shmem_alloc(swap);
  694. spin_unlock(&info->lock);
  695. BUG_ON(page_mapped(page));
  696. swap_writepage(page, wbc);
  697. return 0;
  698. }
  699. spin_unlock(&info->lock);
  700. swapcache_free(swap, NULL);
  701. redirty:
  702. set_page_dirty(page);
  703. if (wbc->for_reclaim)
  704. return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
  705. unlock_page(page);
  706. return 0;
  707. }
  708. #ifdef CONFIG_NUMA
  709. #ifdef CONFIG_TMPFS
  710. static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  711. {
  712. char buffer[64];
  713. if (!mpol || mpol->mode == MPOL_DEFAULT)
  714. return; /* show nothing */
  715. mpol_to_str(buffer, sizeof(buffer), mpol, 1);
  716. seq_printf(seq, ",mpol=%s", buffer);
  717. }
  718. static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  719. {
  720. struct mempolicy *mpol = NULL;
  721. if (sbinfo->mpol) {
  722. spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
  723. mpol = sbinfo->mpol;
  724. mpol_get(mpol);
  725. spin_unlock(&sbinfo->stat_lock);
  726. }
  727. return mpol;
  728. }
  729. #endif /* CONFIG_TMPFS */
  730. static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  731. struct shmem_inode_info *info, pgoff_t index)
  732. {
  733. struct mempolicy mpol, *spol;
  734. struct vm_area_struct pvma;
  735. spol = mpol_cond_copy(&mpol,
  736. mpol_shared_policy_lookup(&info->policy, index));
  737. /* Create a pseudo vma that just contains the policy */
  738. pvma.vm_start = 0;
  739. pvma.vm_pgoff = index;
  740. pvma.vm_ops = NULL;
  741. pvma.vm_policy = spol;
  742. return swapin_readahead(swap, gfp, &pvma, 0);
  743. }
  744. static struct page *shmem_alloc_page(gfp_t gfp,
  745. struct shmem_inode_info *info, pgoff_t index)
  746. {
  747. struct vm_area_struct pvma;
  748. /* Create a pseudo vma that just contains the policy */
  749. pvma.vm_start = 0;
  750. pvma.vm_pgoff = index;
  751. pvma.vm_ops = NULL;
  752. pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index);
  753. /*
  754. * alloc_page_vma() will drop the shared policy reference
  755. */
  756. return alloc_page_vma(gfp, &pvma, 0);
  757. }
  758. #else /* !CONFIG_NUMA */
  759. #ifdef CONFIG_TMPFS
  760. static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
  761. {
  762. }
  763. #endif /* CONFIG_TMPFS */
  764. static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
  765. struct shmem_inode_info *info, pgoff_t index)
  766. {
  767. return swapin_readahead(swap, gfp, NULL, 0);
  768. }
  769. static inline struct page *shmem_alloc_page(gfp_t gfp,
  770. struct shmem_inode_info *info, pgoff_t index)
  771. {
  772. return alloc_page(gfp);
  773. }
  774. #endif /* CONFIG_NUMA */
  775. #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS)
  776. static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
  777. {
  778. return NULL;
  779. }
  780. #endif
  781. /*
  782. * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
  783. *
  784. * If we allocate a new one we do not mark it dirty. That's up to the
  785. * vm. If we swap it in we mark it dirty since we also free the swap
  786. * entry since a page cannot live in both the swap and page cache
  787. */
  788. static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
  789. struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type)
  790. {
  791. struct address_space *mapping = inode->i_mapping;
  792. struct shmem_inode_info *info = SHMEM_I(inode);
  793. struct shmem_sb_info *sbinfo;
  794. struct page *page;
  795. struct page *prealloc_page = NULL;
  796. swp_entry_t swap;
  797. int error;
  798. if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT))
  799. return -EFBIG;
  800. repeat:
  801. page = find_lock_page(mapping, index);
  802. if (page) {
  803. /*
  804. * Once we can get the page lock, it must be uptodate:
  805. * if there were an error in reading back from swap,
  806. * the page would not be inserted into the filecache.
  807. */
  808. BUG_ON(!PageUptodate(page));
  809. goto done;
  810. }
  811. /*
  812. * Try to preload while we can wait, to not make a habit of
  813. * draining atomic reserves; but don't latch on to this cpu.
  814. */
  815. error = radix_tree_preload(gfp & GFP_RECLAIM_MASK);
  816. if (error)
  817. goto out;
  818. radix_tree_preload_end();
  819. if (sgp != SGP_READ && !prealloc_page) {
  820. prealloc_page = shmem_alloc_page(gfp, info, index);
  821. if (prealloc_page) {
  822. SetPageSwapBacked(prealloc_page);
  823. if (mem_cgroup_cache_charge(prealloc_page,
  824. current->mm, GFP_KERNEL)) {
  825. page_cache_release(prealloc_page);
  826. prealloc_page = NULL;
  827. }
  828. }
  829. }
  830. spin_lock(&info->lock);
  831. shmem_recalc_inode(inode);
  832. swap = shmem_get_swap(info, index);
  833. if (swap.val) {
  834. /* Look it up and read it in.. */
  835. page = lookup_swap_cache(swap);
  836. if (!page) {
  837. spin_unlock(&info->lock);
  838. /* here we actually do the io */
  839. if (fault_type)
  840. *fault_type |= VM_FAULT_MAJOR;
  841. page = shmem_swapin(swap, gfp, info, index);
  842. if (!page) {
  843. swp_entry_t nswap = shmem_get_swap(info, index);
  844. if (nswap.val == swap.val) {
  845. error = -ENOMEM;
  846. goto out;
  847. }
  848. goto repeat;
  849. }
  850. wait_on_page_locked(page);
  851. page_cache_release(page);
  852. goto repeat;
  853. }
  854. /* We have to do this with page locked to prevent races */
  855. if (!trylock_page(page)) {
  856. spin_unlock(&info->lock);
  857. wait_on_page_locked(page);
  858. page_cache_release(page);
  859. goto repeat;
  860. }
  861. if (PageWriteback(page)) {
  862. spin_unlock(&info->lock);
  863. wait_on_page_writeback(page);
  864. unlock_page(page);
  865. page_cache_release(page);
  866. goto repeat;
  867. }
  868. if (!PageUptodate(page)) {
  869. spin_unlock(&info->lock);
  870. unlock_page(page);
  871. page_cache_release(page);
  872. error = -EIO;
  873. goto out;
  874. }
  875. error = add_to_page_cache_locked(page, mapping,
  876. index, GFP_NOWAIT);
  877. if (error) {
  878. spin_unlock(&info->lock);
  879. if (error == -ENOMEM) {
  880. /*
  881. * reclaim from proper memory cgroup and
  882. * call memcg's OOM if needed.
  883. */
  884. error = mem_cgroup_shmem_charge_fallback(
  885. page, current->mm, gfp);
  886. if (error) {
  887. unlock_page(page);
  888. page_cache_release(page);
  889. goto out;
  890. }
  891. }
  892. unlock_page(page);
  893. page_cache_release(page);
  894. goto repeat;
  895. }
  896. delete_from_swap_cache(page);
  897. shmem_put_swap(info, index, (swp_entry_t){0});
  898. info->swapped--;
  899. spin_unlock(&info->lock);
  900. set_page_dirty(page);
  901. swap_free(swap);
  902. } else if (sgp == SGP_READ) {
  903. page = find_get_page(mapping, index);
  904. if (page && !trylock_page(page)) {
  905. spin_unlock(&info->lock);
  906. wait_on_page_locked(page);
  907. page_cache_release(page);
  908. goto repeat;
  909. }
  910. spin_unlock(&info->lock);
  911. } else if (prealloc_page) {
  912. sbinfo = SHMEM_SB(inode->i_sb);
  913. if (sbinfo->max_blocks) {
  914. if (percpu_counter_compare(&sbinfo->used_blocks,
  915. sbinfo->max_blocks) >= 0 ||
  916. shmem_acct_block(info->flags))
  917. goto nospace;
  918. percpu_counter_inc(&sbinfo->used_blocks);
  919. inode->i_blocks += BLOCKS_PER_PAGE;
  920. } else if (shmem_acct_block(info->flags))
  921. goto nospace;
  922. page = prealloc_page;
  923. prealloc_page = NULL;
  924. swap = shmem_get_swap(info, index);
  925. if (swap.val)
  926. mem_cgroup_uncharge_cache_page(page);
  927. else
  928. error = add_to_page_cache_lru(page, mapping,
  929. index, GFP_NOWAIT);
  930. /*
  931. * At add_to_page_cache_lru() failure,
  932. * uncharge will be done automatically.
  933. */
  934. if (swap.val || error) {
  935. shmem_unacct_blocks(info->flags, 1);
  936. shmem_free_blocks(inode, 1);
  937. spin_unlock(&info->lock);
  938. page_cache_release(page);
  939. goto repeat;
  940. }
  941. info->alloced++;
  942. spin_unlock(&info->lock);
  943. clear_highpage(page);
  944. flush_dcache_page(page);
  945. SetPageUptodate(page);
  946. if (sgp == SGP_DIRTY)
  947. set_page_dirty(page);
  948. } else {
  949. spin_unlock(&info->lock);
  950. error = -ENOMEM;
  951. goto out;
  952. }
  953. done:
  954. *pagep = page;
  955. error = 0;
  956. out:
  957. if (prealloc_page) {
  958. mem_cgroup_uncharge_cache_page(prealloc_page);
  959. page_cache_release(prealloc_page);
  960. }
  961. return error;
  962. nospace:
  963. /*
  964. * Perhaps the page was brought in from swap between find_lock_page
  965. * and taking info->lock? We allow for that at add_to_page_cache_lru,
  966. * but must also avoid reporting a spurious ENOSPC while working on a
  967. * full tmpfs.
  968. */
  969. page = find_get_page(mapping, index);
  970. spin_unlock(&info->lock);
  971. if (page) {
  972. page_cache_release(page);
  973. goto repeat;
  974. }
  975. error = -ENOSPC;
  976. goto out;
  977. }
  978. static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  979. {
  980. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  981. int error;
  982. int ret = VM_FAULT_LOCKED;
  983. if (((loff_t)vmf->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  984. return VM_FAULT_SIGBUS;
  985. error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret);
  986. if (error)
  987. return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
  988. if (ret & VM_FAULT_MAJOR) {
  989. count_vm_event(PGMAJFAULT);
  990. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  991. }
  992. return ret;
  993. }
  994. #ifdef CONFIG_NUMA
  995. static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
  996. {
  997. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  998. return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
  999. }
  1000. static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
  1001. unsigned long addr)
  1002. {
  1003. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1004. pgoff_t index;
  1005. index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  1006. return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
  1007. }
  1008. #endif
  1009. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  1010. {
  1011. struct inode *inode = file->f_path.dentry->d_inode;
  1012. struct shmem_inode_info *info = SHMEM_I(inode);
  1013. int retval = -ENOMEM;
  1014. spin_lock(&info->lock);
  1015. if (lock && !(info->flags & VM_LOCKED)) {
  1016. if (!user_shm_lock(inode->i_size, user))
  1017. goto out_nomem;
  1018. info->flags |= VM_LOCKED;
  1019. mapping_set_unevictable(file->f_mapping);
  1020. }
  1021. if (!lock && (info->flags & VM_LOCKED) && user) {
  1022. user_shm_unlock(inode->i_size, user);
  1023. info->flags &= ~VM_LOCKED;
  1024. mapping_clear_unevictable(file->f_mapping);
  1025. scan_mapping_unevictable_pages(file->f_mapping);
  1026. }
  1027. retval = 0;
  1028. out_nomem:
  1029. spin_unlock(&info->lock);
  1030. return retval;
  1031. }
  1032. static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
  1033. {
  1034. file_accessed(file);
  1035. vma->vm_ops = &shmem_vm_ops;
  1036. vma->vm_flags |= VM_CAN_NONLINEAR;
  1037. return 0;
  1038. }
  1039. static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
  1040. int mode, dev_t dev, unsigned long flags)
  1041. {
  1042. struct inode *inode;
  1043. struct shmem_inode_info *info;
  1044. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1045. if (shmem_reserve_inode(sb))
  1046. return NULL;
  1047. inode = new_inode(sb);
  1048. if (inode) {
  1049. inode->i_ino = get_next_ino();
  1050. inode_init_owner(inode, dir, mode);
  1051. inode->i_blocks = 0;
  1052. inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
  1053. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  1054. inode->i_generation = get_seconds();
  1055. info = SHMEM_I(inode);
  1056. memset(info, 0, (char *)inode - (char *)info);
  1057. spin_lock_init(&info->lock);
  1058. info->flags = flags & VM_NORESERVE;
  1059. INIT_LIST_HEAD(&info->swaplist);
  1060. INIT_LIST_HEAD(&info->xattr_list);
  1061. cache_no_acl(inode);
  1062. switch (mode & S_IFMT) {
  1063. default:
  1064. inode->i_op = &shmem_special_inode_operations;
  1065. init_special_inode(inode, mode, dev);
  1066. break;
  1067. case S_IFREG:
  1068. inode->i_mapping->a_ops = &shmem_aops;
  1069. inode->i_op = &shmem_inode_operations;
  1070. inode->i_fop = &shmem_file_operations;
  1071. mpol_shared_policy_init(&info->policy,
  1072. shmem_get_sbmpol(sbinfo));
  1073. break;
  1074. case S_IFDIR:
  1075. inc_nlink(inode);
  1076. /* Some things misbehave if size == 0 on a directory */
  1077. inode->i_size = 2 * BOGO_DIRENT_SIZE;
  1078. inode->i_op = &shmem_dir_inode_operations;
  1079. inode->i_fop = &simple_dir_operations;
  1080. break;
  1081. case S_IFLNK:
  1082. /*
  1083. * Must not load anything in the rbtree,
  1084. * mpol_free_shared_policy will not be called.
  1085. */
  1086. mpol_shared_policy_init(&info->policy, NULL);
  1087. break;
  1088. }
  1089. } else
  1090. shmem_free_inode(sb);
  1091. return inode;
  1092. }
  1093. #ifdef CONFIG_TMPFS
  1094. static const struct inode_operations shmem_symlink_inode_operations;
  1095. static const struct inode_operations shmem_symlink_inline_operations;
  1096. static int
  1097. shmem_write_begin(struct file *file, struct address_space *mapping,
  1098. loff_t pos, unsigned len, unsigned flags,
  1099. struct page **pagep, void **fsdata)
  1100. {
  1101. struct inode *inode = mapping->host;
  1102. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1103. return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL);
  1104. }
  1105. static int
  1106. shmem_write_end(struct file *file, struct address_space *mapping,
  1107. loff_t pos, unsigned len, unsigned copied,
  1108. struct page *page, void *fsdata)
  1109. {
  1110. struct inode *inode = mapping->host;
  1111. if (pos + copied > inode->i_size)
  1112. i_size_write(inode, pos + copied);
  1113. set_page_dirty(page);
  1114. unlock_page(page);
  1115. page_cache_release(page);
  1116. return copied;
  1117. }
  1118. static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
  1119. {
  1120. struct inode *inode = filp->f_path.dentry->d_inode;
  1121. struct address_space *mapping = inode->i_mapping;
  1122. pgoff_t index;
  1123. unsigned long offset;
  1124. enum sgp_type sgp = SGP_READ;
  1125. /*
  1126. * Might this read be for a stacking filesystem? Then when reading
  1127. * holes of a sparse file, we actually need to allocate those pages,
  1128. * and even mark them dirty, so it cannot exceed the max_blocks limit.
  1129. */
  1130. if (segment_eq(get_fs(), KERNEL_DS))
  1131. sgp = SGP_DIRTY;
  1132. index = *ppos >> PAGE_CACHE_SHIFT;
  1133. offset = *ppos & ~PAGE_CACHE_MASK;
  1134. for (;;) {
  1135. struct page *page = NULL;
  1136. pgoff_t end_index;
  1137. unsigned long nr, ret;
  1138. loff_t i_size = i_size_read(inode);
  1139. end_index = i_size >> PAGE_CACHE_SHIFT;
  1140. if (index > end_index)
  1141. break;
  1142. if (index == end_index) {
  1143. nr = i_size & ~PAGE_CACHE_MASK;
  1144. if (nr <= offset)
  1145. break;
  1146. }
  1147. desc->error = shmem_getpage(inode, index, &page, sgp, NULL);
  1148. if (desc->error) {
  1149. if (desc->error == -EINVAL)
  1150. desc->error = 0;
  1151. break;
  1152. }
  1153. if (page)
  1154. unlock_page(page);
  1155. /*
  1156. * We must evaluate after, since reads (unlike writes)
  1157. * are called without i_mutex protection against truncate
  1158. */
  1159. nr = PAGE_CACHE_SIZE;
  1160. i_size = i_size_read(inode);
  1161. end_index = i_size >> PAGE_CACHE_SHIFT;
  1162. if (index == end_index) {
  1163. nr = i_size & ~PAGE_CACHE_MASK;
  1164. if (nr <= offset) {
  1165. if (page)
  1166. page_cache_release(page);
  1167. break;
  1168. }
  1169. }
  1170. nr -= offset;
  1171. if (page) {
  1172. /*
  1173. * If users can be writing to this page using arbitrary
  1174. * virtual addresses, take care about potential aliasing
  1175. * before reading the page on the kernel side.
  1176. */
  1177. if (mapping_writably_mapped(mapping))
  1178. flush_dcache_page(page);
  1179. /*
  1180. * Mark the page accessed if we read the beginning.
  1181. */
  1182. if (!offset)
  1183. mark_page_accessed(page);
  1184. } else {
  1185. page = ZERO_PAGE(0);
  1186. page_cache_get(page);
  1187. }
  1188. /*
  1189. * Ok, we have the page, and it's up-to-date, so
  1190. * now we can copy it to user space...
  1191. *
  1192. * The actor routine returns how many bytes were actually used..
  1193. * NOTE! This may not be the same as how much of a user buffer
  1194. * we filled up (we may be padding etc), so we can only update
  1195. * "pos" here (the actor routine has to update the user buffer
  1196. * pointers and the remaining count).
  1197. */
  1198. ret = actor(desc, page, offset, nr);
  1199. offset += ret;
  1200. index += offset >> PAGE_CACHE_SHIFT;
  1201. offset &= ~PAGE_CACHE_MASK;
  1202. page_cache_release(page);
  1203. if (ret != nr || !desc->count)
  1204. break;
  1205. cond_resched();
  1206. }
  1207. *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
  1208. file_accessed(filp);
  1209. }
  1210. static ssize_t shmem_file_aio_read(struct kiocb *iocb,
  1211. const struct iovec *iov, unsigned long nr_segs, loff_t pos)
  1212. {
  1213. struct file *filp = iocb->ki_filp;
  1214. ssize_t retval;
  1215. unsigned long seg;
  1216. size_t count;
  1217. loff_t *ppos = &iocb->ki_pos;
  1218. retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
  1219. if (retval)
  1220. return retval;
  1221. for (seg = 0; seg < nr_segs; seg++) {
  1222. read_descriptor_t desc;
  1223. desc.written = 0;
  1224. desc.arg.buf = iov[seg].iov_base;
  1225. desc.count = iov[seg].iov_len;
  1226. if (desc.count == 0)
  1227. continue;
  1228. desc.error = 0;
  1229. do_shmem_file_read(filp, ppos, &desc, file_read_actor);
  1230. retval += desc.written;
  1231. if (desc.error) {
  1232. retval = retval ?: desc.error;
  1233. break;
  1234. }
  1235. if (desc.count > 0)
  1236. break;
  1237. }
  1238. return retval;
  1239. }
  1240. static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos,
  1241. struct pipe_inode_info *pipe, size_t len,
  1242. unsigned int flags)
  1243. {
  1244. struct address_space *mapping = in->f_mapping;
  1245. struct inode *inode = mapping->host;
  1246. unsigned int loff, nr_pages, req_pages;
  1247. struct page *pages[PIPE_DEF_BUFFERS];
  1248. struct partial_page partial[PIPE_DEF_BUFFERS];
  1249. struct page *page;
  1250. pgoff_t index, end_index;
  1251. loff_t isize, left;
  1252. int error, page_nr;
  1253. struct splice_pipe_desc spd = {
  1254. .pages = pages,
  1255. .partial = partial,
  1256. .flags = flags,
  1257. .ops = &page_cache_pipe_buf_ops,
  1258. .spd_release = spd_release_page,
  1259. };
  1260. isize = i_size_read(inode);
  1261. if (unlikely(*ppos >= isize))
  1262. return 0;
  1263. left = isize - *ppos;
  1264. if (unlikely(left < len))
  1265. len = left;
  1266. if (splice_grow_spd(pipe, &spd))
  1267. return -ENOMEM;
  1268. index = *ppos >> PAGE_CACHE_SHIFT;
  1269. loff = *ppos & ~PAGE_CACHE_MASK;
  1270. req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  1271. nr_pages = min(req_pages, pipe->buffers);
  1272. spd.nr_pages = find_get_pages_contig(mapping, index,
  1273. nr_pages, spd.pages);
  1274. index += spd.nr_pages;
  1275. error = 0;
  1276. while (spd.nr_pages < nr_pages) {
  1277. error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL);
  1278. if (error)
  1279. break;
  1280. unlock_page(page);
  1281. spd.pages[spd.nr_pages++] = page;
  1282. index++;
  1283. }
  1284. index = *ppos >> PAGE_CACHE_SHIFT;
  1285. nr_pages = spd.nr_pages;
  1286. spd.nr_pages = 0;
  1287. for (page_nr = 0; page_nr < nr_pages; page_nr++) {
  1288. unsigned int this_len;
  1289. if (!len)
  1290. break;
  1291. this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff);
  1292. page = spd.pages[page_nr];
  1293. if (!PageUptodate(page) || page->mapping != mapping) {
  1294. error = shmem_getpage(inode, index, &page,
  1295. SGP_CACHE, NULL);
  1296. if (error)
  1297. break;
  1298. unlock_page(page);
  1299. page_cache_release(spd.pages[page_nr]);
  1300. spd.pages[page_nr] = page;
  1301. }
  1302. isize = i_size_read(inode);
  1303. end_index = (isize - 1) >> PAGE_CACHE_SHIFT;
  1304. if (unlikely(!isize || index > end_index))
  1305. break;
  1306. if (end_index == index) {
  1307. unsigned int plen;
  1308. plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1;
  1309. if (plen <= loff)
  1310. break;
  1311. this_len = min(this_len, plen - loff);
  1312. len = this_len;
  1313. }
  1314. spd.partial[page_nr].offset = loff;
  1315. spd.partial[page_nr].len = this_len;
  1316. len -= this_len;
  1317. loff = 0;
  1318. spd.nr_pages++;
  1319. index++;
  1320. }
  1321. while (page_nr < nr_pages)
  1322. page_cache_release(spd.pages[page_nr++]);
  1323. if (spd.nr_pages)
  1324. error = splice_to_pipe(pipe, &spd);
  1325. splice_shrink_spd(pipe, &spd);
  1326. if (error > 0) {
  1327. *ppos += error;
  1328. file_accessed(in);
  1329. }
  1330. return error;
  1331. }
  1332. static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
  1333. {
  1334. struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
  1335. buf->f_type = TMPFS_MAGIC;
  1336. buf->f_bsize = PAGE_CACHE_SIZE;
  1337. buf->f_namelen = NAME_MAX;
  1338. if (sbinfo->max_blocks) {
  1339. buf->f_blocks = sbinfo->max_blocks;
  1340. buf->f_bavail =
  1341. buf->f_bfree = sbinfo->max_blocks -
  1342. percpu_counter_sum(&sbinfo->used_blocks);
  1343. }
  1344. if (sbinfo->max_inodes) {
  1345. buf->f_files = sbinfo->max_inodes;
  1346. buf->f_ffree = sbinfo->free_inodes;
  1347. }
  1348. /* else leave those fields 0 like simple_statfs */
  1349. return 0;
  1350. }
  1351. /*
  1352. * File creation. Allocate an inode, and we're done..
  1353. */
  1354. static int
  1355. shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1356. {
  1357. struct inode *inode;
  1358. int error = -ENOSPC;
  1359. inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
  1360. if (inode) {
  1361. error = security_inode_init_security(inode, dir,
  1362. &dentry->d_name, NULL,
  1363. NULL, NULL);
  1364. if (error) {
  1365. if (error != -EOPNOTSUPP) {
  1366. iput(inode);
  1367. return error;
  1368. }
  1369. }
  1370. #ifdef CONFIG_TMPFS_POSIX_ACL
  1371. error = generic_acl_init(inode, dir);
  1372. if (error) {
  1373. iput(inode);
  1374. return error;
  1375. }
  1376. #else
  1377. error = 0;
  1378. #endif
  1379. dir->i_size += BOGO_DIRENT_SIZE;
  1380. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1381. d_instantiate(dentry, inode);
  1382. dget(dentry); /* Extra count - pin the dentry in core */
  1383. }
  1384. return error;
  1385. }
  1386. static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1387. {
  1388. int error;
  1389. if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
  1390. return error;
  1391. inc_nlink(dir);
  1392. return 0;
  1393. }
  1394. static int shmem_create(struct inode *dir, struct dentry *dentry, int mode,
  1395. struct nameidata *nd)
  1396. {
  1397. return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
  1398. }
  1399. /*
  1400. * Link a file..
  1401. */
  1402. static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1403. {
  1404. struct inode *inode = old_dentry->d_inode;
  1405. int ret;
  1406. /*
  1407. * No ordinary (disk based) filesystem counts links as inodes;
  1408. * but each new link needs a new dentry, pinning lowmem, and
  1409. * tmpfs dentries cannot be pruned until they are unlinked.
  1410. */
  1411. ret = shmem_reserve_inode(inode->i_sb);
  1412. if (ret)
  1413. goto out;
  1414. dir->i_size += BOGO_DIRENT_SIZE;
  1415. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1416. inc_nlink(inode);
  1417. ihold(inode); /* New dentry reference */
  1418. dget(dentry); /* Extra pinning count for the created dentry */
  1419. d_instantiate(dentry, inode);
  1420. out:
  1421. return ret;
  1422. }
  1423. static int shmem_unlink(struct inode *dir, struct dentry *dentry)
  1424. {
  1425. struct inode *inode = dentry->d_inode;
  1426. if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
  1427. shmem_free_inode(inode->i_sb);
  1428. dir->i_size -= BOGO_DIRENT_SIZE;
  1429. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1430. drop_nlink(inode);
  1431. dput(dentry); /* Undo the count from "create" - this does all the work */
  1432. return 0;
  1433. }
  1434. static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
  1435. {
  1436. if (!simple_empty(dentry))
  1437. return -ENOTEMPTY;
  1438. drop_nlink(dentry->d_inode);
  1439. drop_nlink(dir);
  1440. return shmem_unlink(dir, dentry);
  1441. }
  1442. /*
  1443. * The VFS layer already does all the dentry stuff for rename,
  1444. * we just have to decrement the usage count for the target if
  1445. * it exists so that the VFS layer correctly free's it when it
  1446. * gets overwritten.
  1447. */
  1448. static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
  1449. {
  1450. struct inode *inode = old_dentry->d_inode;
  1451. int they_are_dirs = S_ISDIR(inode->i_mode);
  1452. if (!simple_empty(new_dentry))
  1453. return -ENOTEMPTY;
  1454. if (new_dentry->d_inode) {
  1455. (void) shmem_unlink(new_dir, new_dentry);
  1456. if (they_are_dirs)
  1457. drop_nlink(old_dir);
  1458. } else if (they_are_dirs) {
  1459. drop_nlink(old_dir);
  1460. inc_nlink(new_dir);
  1461. }
  1462. old_dir->i_size -= BOGO_DIRENT_SIZE;
  1463. new_dir->i_size += BOGO_DIRENT_SIZE;
  1464. old_dir->i_ctime = old_dir->i_mtime =
  1465. new_dir->i_ctime = new_dir->i_mtime =
  1466. inode->i_ctime = CURRENT_TIME;
  1467. return 0;
  1468. }
  1469. static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1470. {
  1471. int error;
  1472. int len;
  1473. struct inode *inode;
  1474. struct page *page;
  1475. char *kaddr;
  1476. struct shmem_inode_info *info;
  1477. len = strlen(symname) + 1;
  1478. if (len > PAGE_CACHE_SIZE)
  1479. return -ENAMETOOLONG;
  1480. inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
  1481. if (!inode)
  1482. return -ENOSPC;
  1483. error = security_inode_init_security(inode, dir, &dentry->d_name, NULL,
  1484. NULL, NULL);
  1485. if (error) {
  1486. if (error != -EOPNOTSUPP) {
  1487. iput(inode);
  1488. return error;
  1489. }
  1490. error = 0;
  1491. }
  1492. info = SHMEM_I(inode);
  1493. inode->i_size = len-1;
  1494. if (len <= SHMEM_SYMLINK_INLINE_LEN) {
  1495. /* do it inline */
  1496. memcpy(info->inline_symlink, symname, len);
  1497. inode->i_op = &shmem_symlink_inline_operations;
  1498. } else {
  1499. error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL);
  1500. if (error) {
  1501. iput(inode);
  1502. return error;
  1503. }
  1504. inode->i_mapping->a_ops = &shmem_aops;
  1505. inode->i_op = &shmem_symlink_inode_operations;
  1506. kaddr = kmap_atomic(page, KM_USER0);
  1507. memcpy(kaddr, symname, len);
  1508. kunmap_atomic(kaddr, KM_USER0);
  1509. set_page_dirty(page);
  1510. unlock_page(page);
  1511. page_cache_release(page);
  1512. }
  1513. dir->i_size += BOGO_DIRENT_SIZE;
  1514. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  1515. d_instantiate(dentry, inode);
  1516. dget(dentry);
  1517. return 0;
  1518. }
  1519. static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
  1520. {
  1521. nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink);
  1522. return NULL;
  1523. }
  1524. static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
  1525. {
  1526. struct page *page = NULL;
  1527. int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
  1528. nd_set_link(nd, error ? ERR_PTR(error) : kmap(page));
  1529. if (page)
  1530. unlock_page(page);
  1531. return page;
  1532. }
  1533. static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  1534. {
  1535. if (!IS_ERR(nd_get_link(nd))) {
  1536. struct page *page = cookie;
  1537. kunmap(page);
  1538. mark_page_accessed(page);
  1539. page_cache_release(page);
  1540. }
  1541. }
  1542. #ifdef CONFIG_TMPFS_XATTR
  1543. /*
  1544. * Superblocks without xattr inode operations may get some security.* xattr
  1545. * support from the LSM "for free". As soon as we have any other xattrs
  1546. * like ACLs, we also need to implement the security.* handlers at
  1547. * filesystem level, though.
  1548. */
  1549. static int shmem_xattr_get(struct dentry *dentry, const char *name,
  1550. void *buffer, size_t size)
  1551. {
  1552. struct shmem_inode_info *info;
  1553. struct shmem_xattr *xattr;
  1554. int ret = -ENODATA;
  1555. info = SHMEM_I(dentry->d_inode);
  1556. spin_lock(&info->lock);
  1557. list_for_each_entry(xattr, &info->xattr_list, list) {
  1558. if (strcmp(name, xattr->name))
  1559. continue;
  1560. ret = xattr->size;
  1561. if (buffer) {
  1562. if (size < xattr->size)
  1563. ret = -ERANGE;
  1564. else
  1565. memcpy(buffer, xattr->value, xattr->size);
  1566. }
  1567. break;
  1568. }
  1569. spin_unlock(&info->lock);
  1570. return ret;
  1571. }
  1572. static int shmem_xattr_set(struct dentry *dentry, const char *name,
  1573. const void *value, size_t size, int flags)
  1574. {
  1575. struct inode *inode = dentry->d_inode;
  1576. struct shmem_inode_info *info = SHMEM_I(inode);
  1577. struct shmem_xattr *xattr;
  1578. struct shmem_xattr *new_xattr = NULL;
  1579. size_t len;
  1580. int err = 0;
  1581. /* value == NULL means remove */
  1582. if (value) {
  1583. /* wrap around? */
  1584. len = sizeof(*new_xattr) + size;
  1585. if (len <= sizeof(*new_xattr))
  1586. return -ENOMEM;
  1587. new_xattr = kmalloc(len, GFP_KERNEL);
  1588. if (!new_xattr)
  1589. return -ENOMEM;
  1590. new_xattr->name = kstrdup(name, GFP_KERNEL);
  1591. if (!new_xattr->name) {
  1592. kfree(new_xattr);
  1593. return -ENOMEM;
  1594. }
  1595. new_xattr->size = size;
  1596. memcpy(new_xattr->value, value, size);
  1597. }
  1598. spin_lock(&info->lock);
  1599. list_for_each_entry(xattr, &info->xattr_list, list) {
  1600. if (!strcmp(name, xattr->name)) {
  1601. if (flags & XATTR_CREATE) {
  1602. xattr = new_xattr;
  1603. err = -EEXIST;
  1604. } else if (new_xattr) {
  1605. list_replace(&xattr->list, &new_xattr->list);
  1606. } else {
  1607. list_del(&xattr->list);
  1608. }
  1609. goto out;
  1610. }
  1611. }
  1612. if (flags & XATTR_REPLACE) {
  1613. xattr = new_xattr;
  1614. err = -ENODATA;
  1615. } else {
  1616. list_add(&new_xattr->list, &info->xattr_list);
  1617. xattr = NULL;
  1618. }
  1619. out:
  1620. spin_unlock(&info->lock);
  1621. if (xattr)
  1622. kfree(xattr->name);
  1623. kfree(xattr);
  1624. return err;
  1625. }
  1626. static const struct xattr_handler *shmem_xattr_handlers[] = {
  1627. #ifdef CONFIG_TMPFS_POSIX_ACL
  1628. &generic_acl_access_handler,
  1629. &generic_acl_default_handler,
  1630. #endif
  1631. NULL
  1632. };
  1633. static int shmem_xattr_validate(const char *name)
  1634. {
  1635. struct { const char *prefix; size_t len; } arr[] = {
  1636. { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN },
  1637. { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN }
  1638. };
  1639. int i;
  1640. for (i = 0; i < ARRAY_SIZE(arr); i++) {
  1641. size_t preflen = arr[i].len;
  1642. if (strncmp(name, arr[i].prefix, preflen) == 0) {
  1643. if (!name[preflen])
  1644. return -EINVAL;
  1645. return 0;
  1646. }
  1647. }
  1648. return -EOPNOTSUPP;
  1649. }
  1650. static ssize_t shmem_getxattr(struct dentry *dentry, const char *name,
  1651. void *buffer, size_t size)
  1652. {
  1653. int err;
  1654. /*
  1655. * If this is a request for a synthetic attribute in the system.*
  1656. * namespace use the generic infrastructure to resolve a handler
  1657. * for it via sb->s_xattr.
  1658. */
  1659. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1660. return generic_getxattr(dentry, name, buffer, size);
  1661. err = shmem_xattr_validate(name);
  1662. if (err)
  1663. return err;
  1664. return shmem_xattr_get(dentry, name, buffer, size);
  1665. }
  1666. static int shmem_setxattr(struct dentry *dentry, const char *name,
  1667. const void *value, size_t size, int flags)
  1668. {
  1669. int err;
  1670. /*
  1671. * If this is a request for a synthetic attribute in the system.*
  1672. * namespace use the generic infrastructure to resolve a handler
  1673. * for it via sb->s_xattr.
  1674. */
  1675. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1676. return generic_setxattr(dentry, name, value, size, flags);
  1677. err = shmem_xattr_validate(name);
  1678. if (err)
  1679. return err;
  1680. if (size == 0)
  1681. value = ""; /* empty EA, do not remove */
  1682. return shmem_xattr_set(dentry, name, value, size, flags);
  1683. }
  1684. static int shmem_removexattr(struct dentry *dentry, const char *name)
  1685. {
  1686. int err;
  1687. /*
  1688. * If this is a request for a synthetic attribute in the system.*
  1689. * namespace use the generic infrastructure to resolve a handler
  1690. * for it via sb->s_xattr.
  1691. */
  1692. if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
  1693. return generic_removexattr(dentry, name);
  1694. err = shmem_xattr_validate(name);
  1695. if (err)
  1696. return err;
  1697. return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE);
  1698. }
  1699. static bool xattr_is_trusted(const char *name)
  1700. {
  1701. return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
  1702. }
  1703. static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
  1704. {
  1705. bool trusted = capable(CAP_SYS_ADMIN);
  1706. struct shmem_xattr *xattr;
  1707. struct shmem_inode_info *info;
  1708. size_t used = 0;
  1709. info = SHMEM_I(dentry->d_inode);
  1710. spin_lock(&info->lock);
  1711. list_for_each_entry(xattr, &info->xattr_list, list) {
  1712. size_t len;
  1713. /* skip "trusted." attributes for unprivileged callers */
  1714. if (!trusted && xattr_is_trusted(xattr->name))
  1715. continue;
  1716. len = strlen(xattr->name) + 1;
  1717. used += len;
  1718. if (buffer) {
  1719. if (size < used) {
  1720. used = -ERANGE;
  1721. break;
  1722. }
  1723. memcpy(buffer, xattr->name, len);
  1724. buffer += len;
  1725. }
  1726. }
  1727. spin_unlock(&info->lock);
  1728. return used;
  1729. }
  1730. #endif /* CONFIG_TMPFS_XATTR */
  1731. static const struct inode_operations shmem_symlink_inline_operations = {
  1732. .readlink = generic_readlink,
  1733. .follow_link = shmem_follow_link_inline,
  1734. #ifdef CONFIG_TMPFS_XATTR
  1735. .setxattr = shmem_setxattr,
  1736. .getxattr = shmem_getxattr,
  1737. .listxattr = shmem_listxattr,
  1738. .removexattr = shmem_removexattr,
  1739. #endif
  1740. };
  1741. static const struct inode_operations shmem_symlink_inode_operations = {
  1742. .readlink = generic_readlink,
  1743. .follow_link = shmem_follow_link,
  1744. .put_link = shmem_put_link,
  1745. #ifdef CONFIG_TMPFS_XATTR
  1746. .setxattr = shmem_setxattr,
  1747. .getxattr = shmem_getxattr,
  1748. .listxattr = shmem_listxattr,
  1749. .removexattr = shmem_removexattr,
  1750. #endif
  1751. };
  1752. static struct dentry *shmem_get_parent(struct dentry *child)
  1753. {
  1754. return ERR_PTR(-ESTALE);
  1755. }
  1756. static int shmem_match(struct inode *ino, void *vfh)
  1757. {
  1758. __u32 *fh = vfh;
  1759. __u64 inum = fh[2];
  1760. inum = (inum << 32) | fh[1];
  1761. return ino->i_ino == inum && fh[0] == ino->i_generation;
  1762. }
  1763. static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
  1764. struct fid *fid, int fh_len, int fh_type)
  1765. {
  1766. struct inode *inode;
  1767. struct dentry *dentry = NULL;
  1768. u64 inum = fid->raw[2];
  1769. inum = (inum << 32) | fid->raw[1];
  1770. if (fh_len < 3)
  1771. return NULL;
  1772. inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
  1773. shmem_match, fid->raw);
  1774. if (inode) {
  1775. dentry = d_find_alias(inode);
  1776. iput(inode);
  1777. }
  1778. return dentry;
  1779. }
  1780. static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  1781. int connectable)
  1782. {
  1783. struct inode *inode = dentry->d_inode;
  1784. if (*len < 3) {
  1785. *len = 3;
  1786. return 255;
  1787. }
  1788. if (inode_unhashed(inode)) {
  1789. /* Unfortunately insert_inode_hash is not idempotent,
  1790. * so as we hash inodes here rather than at creation
  1791. * time, we need a lock to ensure we only try
  1792. * to do it once
  1793. */
  1794. static DEFINE_SPINLOCK(lock);
  1795. spin_lock(&lock);
  1796. if (inode_unhashed(inode))
  1797. __insert_inode_hash(inode,
  1798. inode->i_ino + inode->i_generation);
  1799. spin_unlock(&lock);
  1800. }
  1801. fh[0] = inode->i_generation;
  1802. fh[1] = inode->i_ino;
  1803. fh[2] = ((__u64)inode->i_ino) >> 32;
  1804. *len = 3;
  1805. return 1;
  1806. }
  1807. static const struct export_operations shmem_export_ops = {
  1808. .get_parent = shmem_get_parent,
  1809. .encode_fh = shmem_encode_fh,
  1810. .fh_to_dentry = shmem_fh_to_dentry,
  1811. };
  1812. static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
  1813. bool remount)
  1814. {
  1815. char *this_char, *value, *rest;
  1816. while (options != NULL) {
  1817. this_char = options;
  1818. for (;;) {
  1819. /*
  1820. * NUL-terminate this option: unfortunately,
  1821. * mount options form a comma-separated list,
  1822. * but mpol's nodelist may also contain commas.
  1823. */
  1824. options = strchr(options, ',');
  1825. if (options == NULL)
  1826. break;
  1827. options++;
  1828. if (!isdigit(*options)) {
  1829. options[-1] = '\0';
  1830. break;
  1831. }
  1832. }
  1833. if (!*this_char)
  1834. continue;
  1835. if ((value = strchr(this_char,'=')) != NULL) {
  1836. *value++ = 0;
  1837. } else {
  1838. printk(KERN_ERR
  1839. "tmpfs: No value for mount option '%s'\n",
  1840. this_char);
  1841. return 1;
  1842. }
  1843. if (!strcmp(this_char,"size")) {
  1844. unsigned long long size;
  1845. size = memparse(value,&rest);
  1846. if (*rest == '%') {
  1847. size <<= PAGE_SHIFT;
  1848. size *= totalram_pages;
  1849. do_div(size, 100);
  1850. rest++;
  1851. }
  1852. if (*rest)
  1853. goto bad_val;
  1854. sbinfo->max_blocks =
  1855. DIV_ROUND_UP(size, PAGE_CACHE_SIZE);
  1856. } else if (!strcmp(this_char,"nr_blocks")) {
  1857. sbinfo->max_blocks = memparse(value, &rest);
  1858. if (*rest)
  1859. goto bad_val;
  1860. } else if (!strcmp(this_char,"nr_inodes")) {
  1861. sbinfo->max_inodes = memparse(value, &rest);
  1862. if (*rest)
  1863. goto bad_val;
  1864. } else if (!strcmp(this_char,"mode")) {
  1865. if (remount)
  1866. continue;
  1867. sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
  1868. if (*rest)
  1869. goto bad_val;
  1870. } else if (!strcmp(this_char,"uid")) {
  1871. if (remount)
  1872. continue;
  1873. sbinfo->uid = simple_strtoul(value, &rest, 0);
  1874. if (*rest)
  1875. goto bad_val;
  1876. } else if (!strcmp(this_char,"gid")) {
  1877. if (remount)
  1878. continue;
  1879. sbinfo->gid = simple_strtoul(value, &rest, 0);
  1880. if (*rest)
  1881. goto bad_val;
  1882. } else if (!strcmp(this_char,"mpol")) {
  1883. if (mpol_parse_str(value, &sbinfo->mpol, 1))
  1884. goto bad_val;
  1885. } else {
  1886. printk(KERN_ERR "tmpfs: Bad mount option %s\n",
  1887. this_char);
  1888. return 1;
  1889. }
  1890. }
  1891. return 0;
  1892. bad_val:
  1893. printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n",
  1894. value, this_char);
  1895. return 1;
  1896. }
  1897. static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
  1898. {
  1899. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1900. struct shmem_sb_info config = *sbinfo;
  1901. unsigned long inodes;
  1902. int error = -EINVAL;
  1903. if (shmem_parse_options(data, &config, true))
  1904. return error;
  1905. spin_lock(&sbinfo->stat_lock);
  1906. inodes = sbinfo->max_inodes - sbinfo->free_inodes;
  1907. if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
  1908. goto out;
  1909. if (config.max_inodes < inodes)
  1910. goto out;
  1911. /*
  1912. * Those tests also disallow limited->unlimited while any are in
  1913. * use, so i_blocks will always be zero when max_blocks is zero;
  1914. * but we must separately disallow unlimited->limited, because
  1915. * in that case we have no record of how much is already in use.
  1916. */
  1917. if (config.max_blocks && !sbinfo->max_blocks)
  1918. goto out;
  1919. if (config.max_inodes && !sbinfo->max_inodes)
  1920. goto out;
  1921. error = 0;
  1922. sbinfo->max_blocks = config.max_blocks;
  1923. sbinfo->max_inodes = config.max_inodes;
  1924. sbinfo->free_inodes = config.max_inodes - inodes;
  1925. mpol_put(sbinfo->mpol);
  1926. sbinfo->mpol = config.mpol; /* transfers initial ref */
  1927. out:
  1928. spin_unlock(&sbinfo->stat_lock);
  1929. return error;
  1930. }
  1931. static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs)
  1932. {
  1933. struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb);
  1934. if (sbinfo->max_blocks != shmem_default_max_blocks())
  1935. seq_printf(seq, ",size=%luk",
  1936. sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10));
  1937. if (sbinfo->max_inodes != shmem_default_max_inodes())
  1938. seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
  1939. if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
  1940. seq_printf(seq, ",mode=%03o", sbinfo->mode);
  1941. if (sbinfo->uid != 0)
  1942. seq_printf(seq, ",uid=%u", sbinfo->uid);
  1943. if (sbinfo->gid != 0)
  1944. seq_printf(seq, ",gid=%u", sbinfo->gid);
  1945. shmem_show_mpol(seq, sbinfo->mpol);
  1946. return 0;
  1947. }
  1948. #endif /* CONFIG_TMPFS */
  1949. static void shmem_put_super(struct super_block *sb)
  1950. {
  1951. struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
  1952. percpu_counter_destroy(&sbinfo->used_blocks);
  1953. kfree(sbinfo);
  1954. sb->s_fs_info = NULL;
  1955. }
  1956. int shmem_fill_super(struct super_block *sb, void *data, int silent)
  1957. {
  1958. struct inode *inode;
  1959. struct dentry *root;
  1960. struct shmem_sb_info *sbinfo;
  1961. int err = -ENOMEM;
  1962. /* Round up to L1_CACHE_BYTES to resist false sharing */
  1963. sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
  1964. L1_CACHE_BYTES), GFP_KERNEL);
  1965. if (!sbinfo)
  1966. return -ENOMEM;
  1967. sbinfo->mode = S_IRWXUGO | S_ISVTX;
  1968. sbinfo->uid = current_fsuid();
  1969. sbinfo->gid = current_fsgid();
  1970. sb->s_fs_info = sbinfo;
  1971. #ifdef CONFIG_TMPFS
  1972. /*
  1973. * Per default we only allow half of the physical ram per
  1974. * tmpfs instance, limiting inodes to one per page of lowmem;
  1975. * but the internal instance is left unlimited.
  1976. */
  1977. if (!(sb->s_flags & MS_NOUSER)) {
  1978. sbinfo->max_blocks = shmem_default_max_blocks();
  1979. sbinfo->max_inodes = shmem_default_max_inodes();
  1980. if (shmem_parse_options(data, sbinfo, false)) {
  1981. err = -EINVAL;
  1982. goto failed;
  1983. }
  1984. }
  1985. sb->s_export_op = &shmem_export_ops;
  1986. #else
  1987. sb->s_flags |= MS_NOUSER;
  1988. #endif
  1989. spin_lock_init(&sbinfo->stat_lock);
  1990. if (percpu_counter_init(&sbinfo->used_blocks, 0))
  1991. goto failed;
  1992. sbinfo->free_inodes = sbinfo->max_inodes;
  1993. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1994. sb->s_blocksize = PAGE_CACHE_SIZE;
  1995. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1996. sb->s_magic = TMPFS_MAGIC;
  1997. sb->s_op = &shmem_ops;
  1998. sb->s_time_gran = 1;
  1999. #ifdef CONFIG_TMPFS_XATTR
  2000. sb->s_xattr = shmem_xattr_handlers;
  2001. #endif
  2002. #ifdef CONFIG_TMPFS_POSIX_ACL
  2003. sb->s_flags |= MS_POSIXACL;
  2004. #endif
  2005. inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
  2006. if (!inode)
  2007. goto failed;
  2008. inode->i_uid = sbinfo->uid;
  2009. inode->i_gid = sbinfo->gid;
  2010. root = d_alloc_root(inode);
  2011. if (!root)
  2012. goto failed_iput;
  2013. sb->s_root = root;
  2014. return 0;
  2015. failed_iput:
  2016. iput(inode);
  2017. failed:
  2018. shmem_put_super(sb);
  2019. return err;
  2020. }
  2021. static struct kmem_cache *shmem_inode_cachep;
  2022. static struct inode *shmem_alloc_inode(struct super_block *sb)
  2023. {
  2024. struct shmem_inode_info *info;
  2025. info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
  2026. if (!info)
  2027. return NULL;
  2028. return &info->vfs_inode;
  2029. }
  2030. static void shmem_destroy_callback(struct rcu_head *head)
  2031. {
  2032. struct inode *inode = container_of(head, struct inode, i_rcu);
  2033. INIT_LIST_HEAD(&inode->i_dentry);
  2034. kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
  2035. }
  2036. static void shmem_destroy_inode(struct inode *inode)
  2037. {
  2038. if ((inode->i_mode & S_IFMT) == S_IFREG) {
  2039. /* only struct inode is valid if it's an inline symlink */
  2040. mpol_free_shared_policy(&SHMEM_I(inode)->policy);
  2041. }
  2042. call_rcu(&inode->i_rcu, shmem_destroy_callback);
  2043. }
  2044. static void shmem_init_inode(void *foo)
  2045. {
  2046. struct shmem_inode_info *info = foo;
  2047. inode_init_once(&info->vfs_inode);
  2048. }
  2049. static int shmem_init_inodecache(void)
  2050. {
  2051. shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
  2052. sizeof(struct shmem_inode_info),
  2053. 0, SLAB_PANIC, shmem_init_inode);
  2054. return 0;
  2055. }
  2056. static void shmem_destroy_inodecache(void)
  2057. {
  2058. kmem_cache_destroy(shmem_inode_cachep);
  2059. }
  2060. static const struct address_space_operations shmem_aops = {
  2061. .writepage = shmem_writepage,
  2062. .set_page_dirty = __set_page_dirty_no_writeback,
  2063. #ifdef CONFIG_TMPFS
  2064. .write_begin = shmem_write_begin,
  2065. .write_end = shmem_write_end,
  2066. #endif
  2067. .migratepage = migrate_page,
  2068. .error_remove_page = generic_error_remove_page,
  2069. };
  2070. static const struct file_operations shmem_file_operations = {
  2071. .mmap = shmem_mmap,
  2072. #ifdef CONFIG_TMPFS
  2073. .llseek = generic_file_llseek,
  2074. .read = do_sync_read,
  2075. .write = do_sync_write,
  2076. .aio_read = shmem_file_aio_read,
  2077. .aio_write = generic_file_aio_write,
  2078. .fsync = noop_fsync,
  2079. .splice_read = shmem_file_splice_read,
  2080. .splice_write = generic_file_splice_write,
  2081. #endif
  2082. };
  2083. static const struct inode_operations shmem_inode_operations = {
  2084. .setattr = shmem_setattr,
  2085. .truncate_range = shmem_truncate_range,
  2086. #ifdef CONFIG_TMPFS_XATTR
  2087. .setxattr = shmem_setxattr,
  2088. .getxattr = shmem_getxattr,
  2089. .listxattr = shmem_listxattr,
  2090. .removexattr = shmem_removexattr,
  2091. #endif
  2092. };
  2093. static const struct inode_operations shmem_dir_inode_operations = {
  2094. #ifdef CONFIG_TMPFS
  2095. .create = shmem_create,
  2096. .lookup = simple_lookup,
  2097. .link = shmem_link,
  2098. .unlink = shmem_unlink,
  2099. .symlink = shmem_symlink,
  2100. .mkdir = shmem_mkdir,
  2101. .rmdir = shmem_rmdir,
  2102. .mknod = shmem_mknod,
  2103. .rename = shmem_rename,
  2104. #endif
  2105. #ifdef CONFIG_TMPFS_XATTR
  2106. .setxattr = shmem_setxattr,
  2107. .getxattr = shmem_getxattr,
  2108. .listxattr = shmem_listxattr,
  2109. .removexattr = shmem_removexattr,
  2110. #endif
  2111. #ifdef CONFIG_TMPFS_POSIX_ACL
  2112. .setattr = shmem_setattr,
  2113. #endif
  2114. };
  2115. static const struct inode_operations shmem_special_inode_operations = {
  2116. #ifdef CONFIG_TMPFS_XATTR
  2117. .setxattr = shmem_setxattr,
  2118. .getxattr = shmem_getxattr,
  2119. .listxattr = shmem_listxattr,
  2120. .removexattr = shmem_removexattr,
  2121. #endif
  2122. #ifdef CONFIG_TMPFS_POSIX_ACL
  2123. .setattr = shmem_setattr,
  2124. #endif
  2125. };
  2126. static const struct super_operations shmem_ops = {
  2127. .alloc_inode = shmem_alloc_inode,
  2128. .destroy_inode = shmem_destroy_inode,
  2129. #ifdef CONFIG_TMPFS
  2130. .statfs = shmem_statfs,
  2131. .remount_fs = shmem_remount_fs,
  2132. .show_options = shmem_show_options,
  2133. #endif
  2134. .evict_inode = shmem_evict_inode,
  2135. .drop_inode = generic_delete_inode,
  2136. .put_super = shmem_put_super,
  2137. };
  2138. static const struct vm_operations_struct shmem_vm_ops = {
  2139. .fault = shmem_fault,
  2140. #ifdef CONFIG_NUMA
  2141. .set_policy = shmem_set_policy,
  2142. .get_policy = shmem_get_policy,
  2143. #endif
  2144. };
  2145. static struct dentry *shmem_mount(struct file_system_type *fs_type,
  2146. int flags, const char *dev_name, void *data)
  2147. {
  2148. return mount_nodev(fs_type, flags, data, shmem_fill_super);
  2149. }
  2150. static struct file_system_type shmem_fs_type = {
  2151. .owner = THIS_MODULE,
  2152. .name = "tmpfs",
  2153. .mount = shmem_mount,
  2154. .kill_sb = kill_litter_super,
  2155. };
  2156. int __init shmem_init(void)
  2157. {
  2158. int error;
  2159. error = bdi_init(&shmem_backing_dev_info);
  2160. if (error)
  2161. goto out4;
  2162. error = shmem_init_inodecache();
  2163. if (error)
  2164. goto out3;
  2165. error = register_filesystem(&shmem_fs_type);
  2166. if (error) {
  2167. printk(KERN_ERR "Could not register tmpfs\n");
  2168. goto out2;
  2169. }
  2170. shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER,
  2171. shmem_fs_type.name, NULL);
  2172. if (IS_ERR(shm_mnt)) {
  2173. error = PTR_ERR(shm_mnt);
  2174. printk(KERN_ERR "Could not kern_mount tmpfs\n");
  2175. goto out1;
  2176. }
  2177. return 0;
  2178. out1:
  2179. unregister_filesystem(&shmem_fs_type);
  2180. out2:
  2181. shmem_destroy_inodecache();
  2182. out3:
  2183. bdi_destroy(&shmem_backing_dev_info);
  2184. out4:
  2185. shm_mnt = ERR_PTR(error);
  2186. return error;
  2187. }
  2188. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  2189. /**
  2190. * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
  2191. * @inode: the inode to be searched
  2192. * @index: the page offset to be searched
  2193. * @pagep: the pointer for the found page to be stored
  2194. * @swapp: the pointer for the found swap entry to be stored
  2195. *
  2196. * If a page is found, refcount of it is incremented. Callers should handle
  2197. * these refcount.
  2198. */
  2199. void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
  2200. struct page **pagep, swp_entry_t *swapp)
  2201. {
  2202. struct shmem_inode_info *info = SHMEM_I(inode);
  2203. struct page *page = NULL;
  2204. swp_entry_t swap = {0};
  2205. if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  2206. goto out;
  2207. spin_lock(&info->lock);
  2208. #ifdef CONFIG_SWAP
  2209. swap = shmem_get_swap(info, index);
  2210. if (swap.val)
  2211. page = find_get_page(&swapper_space, swap.val);
  2212. else
  2213. #endif
  2214. page = find_get_page(inode->i_mapping, index);
  2215. spin_unlock(&info->lock);
  2216. out:
  2217. *pagep = page;
  2218. *swapp = swap;
  2219. }
  2220. #endif
  2221. #else /* !CONFIG_SHMEM */
  2222. /*
  2223. * tiny-shmem: simple shmemfs and tmpfs using ramfs code
  2224. *
  2225. * This is intended for small system where the benefits of the full
  2226. * shmem code (swap-backed and resource-limited) are outweighed by
  2227. * their complexity. On systems without swap this code should be
  2228. * effectively equivalent, but much lighter weight.
  2229. */
  2230. #include <linux/ramfs.h>
  2231. static struct file_system_type shmem_fs_type = {
  2232. .name = "tmpfs",
  2233. .mount = ramfs_mount,
  2234. .kill_sb = kill_litter_super,
  2235. };
  2236. int __init shmem_init(void)
  2237. {
  2238. BUG_ON(register_filesystem(&shmem_fs_type) != 0);
  2239. shm_mnt = kern_mount(&shmem_fs_type);
  2240. BUG_ON(IS_ERR(shm_mnt));
  2241. return 0;
  2242. }
  2243. int shmem_unuse(swp_entry_t swap, struct page *page)
  2244. {
  2245. return 0;
  2246. }
  2247. int shmem_lock(struct file *file, int lock, struct user_struct *user)
  2248. {
  2249. return 0;
  2250. }
  2251. void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
  2252. {
  2253. truncate_inode_pages_range(inode->i_mapping, lstart, lend);
  2254. }
  2255. EXPORT_SYMBOL_GPL(shmem_truncate_range);
  2256. #ifdef CONFIG_CGROUP_MEM_RES_CTLR
  2257. /**
  2258. * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file
  2259. * @inode: the inode to be searched
  2260. * @index: the page offset to be searched
  2261. * @pagep: the pointer for the found page to be stored
  2262. * @swapp: the pointer for the found swap entry to be stored
  2263. *
  2264. * If a page is found, refcount of it is incremented. Callers should handle
  2265. * these refcount.
  2266. */
  2267. void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index,
  2268. struct page **pagep, swp_entry_t *swapp)
  2269. {
  2270. struct page *page = NULL;
  2271. if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode))
  2272. goto out;
  2273. page = find_get_page(inode->i_mapping, index);
  2274. out:
  2275. *pagep = page;
  2276. *swapp = (swp_entry_t){0};
  2277. }
  2278. #endif
  2279. #define shmem_vm_ops generic_file_vm_ops
  2280. #define shmem_file_operations ramfs_file_operations
  2281. #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
  2282. #define shmem_acct_size(flags, size) 0
  2283. #define shmem_unacct_size(flags, size) do {} while (0)
  2284. #endif /* CONFIG_SHMEM */
  2285. /* common code */
  2286. /**
  2287. * shmem_file_setup - get an unlinked file living in tmpfs
  2288. * @name: name for dentry (to be seen in /proc/<pid>/maps
  2289. * @size: size to be set for the file
  2290. * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
  2291. */
  2292. struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
  2293. {
  2294. int error;
  2295. struct file *file;
  2296. struct inode *inode;
  2297. struct path path;
  2298. struct dentry *root;
  2299. struct qstr this;
  2300. if (IS_ERR(shm_mnt))
  2301. return (void *)shm_mnt;
  2302. if (size < 0 || size > MAX_LFS_FILESIZE)
  2303. return ERR_PTR(-EINVAL);
  2304. if (shmem_acct_size(flags, size))
  2305. return ERR_PTR(-ENOMEM);
  2306. error = -ENOMEM;
  2307. this.name = name;
  2308. this.len = strlen(name);
  2309. this.hash = 0; /* will go */
  2310. root = shm_mnt->mnt_root;
  2311. path.dentry = d_alloc(root, &this);
  2312. if (!path.dentry)
  2313. goto put_memory;
  2314. path.mnt = mntget(shm_mnt);
  2315. error = -ENOSPC;
  2316. inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
  2317. if (!inode)
  2318. goto put_dentry;
  2319. d_instantiate(path.dentry, inode);
  2320. inode->i_size = size;
  2321. inode->i_nlink = 0; /* It is unlinked */
  2322. #ifndef CONFIG_MMU
  2323. error = ramfs_nommu_expand_for_mapping(inode, size);
  2324. if (error)
  2325. goto put_dentry;
  2326. #endif
  2327. error = -ENFILE;
  2328. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  2329. &shmem_file_operations);
  2330. if (!file)
  2331. goto put_dentry;
  2332. return file;
  2333. put_dentry:
  2334. path_put(&path);
  2335. put_memory:
  2336. shmem_unacct_size(flags, size);
  2337. return ERR_PTR(error);
  2338. }
  2339. EXPORT_SYMBOL_GPL(shmem_file_setup);
  2340. /**
  2341. * shmem_zero_setup - setup a shared anonymous mapping
  2342. * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
  2343. */
  2344. int shmem_zero_setup(struct vm_area_struct *vma)
  2345. {
  2346. struct file *file;
  2347. loff_t size = vma->vm_end - vma->vm_start;
  2348. file = shmem_file_setup("dev/zero", size, vma->vm_flags);
  2349. if (IS_ERR(file))
  2350. return PTR_ERR(file);
  2351. if (vma->vm_file)
  2352. fput(vma->vm_file);
  2353. vma->vm_file = file;
  2354. vma->vm_ops = &shmem_vm_ops;
  2355. vma->vm_flags |= VM_CAN_NONLINEAR;
  2356. return 0;
  2357. }
  2358. /**
  2359. * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
  2360. * @mapping: the page's address_space
  2361. * @index: the page index
  2362. * @gfp: the page allocator flags to use if allocating
  2363. *
  2364. * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
  2365. * with any new page allocations done using the specified allocation flags.
  2366. * But read_cache_page_gfp() uses the ->readpage() method: which does not
  2367. * suit tmpfs, since it may have pages in swapcache, and needs to find those
  2368. * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
  2369. *
  2370. * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
  2371. * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
  2372. */
  2373. struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
  2374. pgoff_t index, gfp_t gfp)
  2375. {
  2376. #ifdef CONFIG_SHMEM
  2377. struct inode *inode = mapping->host;
  2378. struct page *page;
  2379. int error;
  2380. BUG_ON(mapping->a_ops != &shmem_aops);
  2381. error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL);
  2382. if (error)
  2383. page = ERR_PTR(error);
  2384. else
  2385. unlock_page(page);
  2386. return page;
  2387. #else
  2388. /*
  2389. * The tiny !SHMEM case uses ramfs without swap
  2390. */
  2391. return read_cache_page_gfp(mapping, index, gfp);
  2392. #endif
  2393. }
  2394. EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);