shmem.c 78 KB

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