shmem.c 65 KB

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