shmem.c 77 KB

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