summaryrefslogtreecommitdiff
path: root/nuttx/fs/smartfs/smartfs_procfs.c
blob: 2b56f366d97ceb926075b8920bfaf5df74655f35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
/****************************************************************************
 * fs/smartfs/smartfs_procfs.c
 *
 *   Copyright (C) 2013-2014 Ken Pettit. All rights reserved.
 *   Author: Ken Pettit <pettitkd@gmail.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>
#include <sys/statfs.h>
#include <sys/stat.h>

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>

#include <nuttx/arch.h>
#include <nuttx/sched.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h>
#include <nuttx/fs/dirent.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/mtd/smart.h>

#include <arch/irq.h>
#include "smartfs.h"

#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_EXCLUDE_SMARTFS)

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Private Types
 ****************************************************************************/
/* This enumeration identifies all of the thread attributes that can be
 * accessed via the procfs file system.
 */

/* Level 1 is the directory of attributes */

struct smartfs_level1_s
{
  struct procfs_dir_priv_s  base;    /* Base directory private data */

  /* Add context specific data types here for managing the directory
   * open / read / stat, etc.
   */

  struct smartfs_mountpt_s*  mount;
  uint8_t direntry;
};

/* This structure describes one open "file" */

struct smartfs_file_s
{
  struct procfs_file_s  base;        /* Base open file structure */

  /* Add context specific data types for managing an open file here */

  struct smartfs_level1_s level1;    /* Reference to item being accessed */
  uint16_t  offset;
};

struct smartfs_procfs_entry_s
{
  const char  *name;                 /* Name of the directory entry */
  size_t (*read)(FAR struct file *filep, FAR char *buffer, size_t buflen);
  uint8_t type;
};

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/
/* File system methods */

static int      smartfs_open(FAR struct file *filep, FAR const char *relpath,
                  int oflags, mode_t mode);
static int      smartfs_close(FAR struct file *filep);
static ssize_t  smartfs_read(FAR struct file *filep, FAR char *buffer,
                  size_t buflen);

static int      smartfs_dup(FAR const struct file *oldp,
                 FAR struct file *newp);

static int      smartfs_opendir(const char *relpath, FAR struct fs_dirent_s *dir);
static int      smartfs_closedir(FAR struct fs_dirent_s *dir);
static int      smartfs_readdir(FAR struct fs_dirent_s *dir);
static int      smartfs_rewinddir(FAR struct fs_dirent_s *dir);

static int      smartfs_stat(FAR const char *relpath, FAR struct stat *buf);

static size_t   smartfs_status_read(FAR struct file *filep, FAR char *buffer,
                  size_t buflen);
#ifdef CONFIG_MTD_SMART_ALLOC_DEBUG
static size_t   smartfs_mem_read(FAR struct file *filep, FAR char *buffer,
                  size_t buflen);
#endif
#ifdef CONFIG_MTD_SMART_SECTOR_ERASE_DEBUG
static size_t   smartfs_erasemap_read(FAR struct file *filep, FAR char *buffer,
                  size_t buflen);
#endif
#ifdef CONFIG_SMARTFS_FILE_SECTOR_DEBUG
static size_t   smartfs_files_read(FAR struct file *filep, FAR char *buffer,
                  size_t buflen);
#endif

/****************************************************************************
 * Private Variables
 ****************************************************************************/

static const struct smartfs_procfs_entry_s g_direntry[] =
{
#ifdef CONFIG_MTD_SMART_SECTOR_ERASE_DEBUG
  { "erasemap", smartfs_erasemap_read, DTYPE_FILE },
#endif
#ifdef CONFIG_MTD_SMART_ALLOC_DEBUG
  { "mem",      smartfs_mem_read, DTYPE_FILE },
#endif
  { "status",   smartfs_status_read, DTYPE_FILE }
};

static const uint8_t g_direntrycount = sizeof(g_direntry) /
                      sizeof(struct smartfs_procfs_entry_s);

/****************************************************************************
 * Public Variables
 ****************************************************************************/

/* See include/nutts/fs/procfs.h
 * We use the old-fashioned kind of initializers so that this will compile
 * with any compiler.
 */

const struct procfs_operations smartfs_procfsoperations =
{
  smartfs_open,       /* open */
  smartfs_close,      /* close */
  smartfs_read,       /* read */

  /* No write supported */
  NULL,               /* write */

  smartfs_dup,        /* dup */

  smartfs_opendir,    /* opendir */
  smartfs_closedir,   /* closedir */
  smartfs_readdir,    /* readdir */
  smartfs_rewinddir,  /* rewinddir */

  smartfs_stat        /* stat */
};

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: smartfs_find_dirref
 *
 * Description:
 *   Analyse relpath to find the directory reference entry it represents,
 *   if any.
 *
 ****************************************************************************/

static int smartfs_find_dirref(FAR const char *relpath,
            FAR struct smartfs_level1_s *level1)
{
  int         ret = -ENOENT;
  FAR struct  smartfs_mountpt_s* mount;
  uint16_t    x;
  FAR char *  str;

  mount = smartfs_get_first_mount();

  /* Skip the "fs/smartfs" portion of relpath */

  if (strncmp(relpath, "fs/smartfs", 10) == 0)
    {
      relpath += 10;
    }

  if (relpath[0] == '/')
    {
      relpath++;
    }

  /* Now test if doing a full dir listing of fs/smartfs */

  if (relpath[0] == '\0')
    {
      /* Save the mount as the first one to display */

      level1->mount = mount;
      level1->base.level    = 1;
      level1->base.nentries = 0;
      while (mount != NULL)
        {
          level1->base.nentries++;
          mount = mount->fs_next;
        }

      level1->base.index    = 0;
      ret = OK;
    }
  else
    {
      /* Search for the requested entry */

      str = strchr(relpath, '/');
      if (str)
        {
          x = str - relpath;
        }
      else
        {
          x = strlen(relpath);
        }

      while (mount)
        {
          if (strncmp(mount->fs_blkdriver->i_name, relpath, x) == 0)
            {
              /* Found the mount point.  Just break */

              break;
            }

          /* Try the next mount */

          mount = mount->fs_next;
        }

      if (mount)
        {
          /* Save the mount and skip it in the relpath */

          ret = OK;
          level1->mount = mount;
          relpath += strlen(mount->fs_blkdriver->i_name);
          if (relpath[0] == '/')
            {
              relpath++;
            }

          /* Test if a level 3 directory entry being requested or not */

          if (relpath[0] == '\0')
            {
              /* Requesting directory listing of a specific SMARTFS mount or entry */

              level1->base.level    = 2;
              level1->base.nentries = g_direntrycount;
              level1->base.index    = 0;
            }
          else
            {
              /* Find the level 3 directory entry */

              level1->base.level    = 3;
              level1->base.nentries = 1;
              level1->base.index    = 0;
              level1->direntry      = 0;

              while (level1->direntry < g_direntrycount)
                {
                  /* Test if this entry matches */

                  if (strcmp(relpath, g_direntry[level1->direntry].name) == 0)
                    {
                      break;
                    }

                  /* Advance to next entry */

                  level1->direntry++;
                }

              /* Test if entry found or not */

              if (level1->direntry == g_direntrycount)
                {
                  ret = -ENOENT;
                }
            }
        }
    }

  return ret;
}

/****************************************************************************
 * Name: smartfs_open
 ****************************************************************************/

static int smartfs_open(FAR struct file *filep, FAR const char *relpath,
                      int oflags, mode_t mode)
{
  FAR struct smartfs_file_s *priv;
  int   ret;

  fvdbg("Open '%s'\n", relpath);

  /* PROCFS is read-only.  Any attempt to open with any kind of write
   * access is not permitted.
   *
   * REVISIT:  Write-able proc files could be quite useful.
   */

  if (((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0) &&
      (smartfs_procfsoperations.write == NULL))
    {
      fdbg("ERROR: Only O_RDONLY supported\n");
      return -EACCES;
    }

  /* Allocate a container to hold the task and attribute selection */

  priv = (FAR struct smartfs_file_s *)kmm_malloc(sizeof(struct smartfs_file_s));
  if (!priv)
    {
      fdbg("ERROR: Failed to allocate file attributes\n");
      return -ENOMEM;
    }

  /* Find the directory entry being opened */

  ret = smartfs_find_dirref(relpath, &priv->level1);
  if (ret == -ENOENT)
    {
      /* Entry not found */

      kmm_free(priv);
      return ret;
    }

  priv->offset = 0;

  /* Save the index as the open-specific state in filep->f_priv */

  filep->f_priv = (FAR void *)priv;
  return OK;
}

/****************************************************************************
 * Name: smartfs_close
 ****************************************************************************/

static int smartfs_close(FAR struct file *filep)
{
  FAR struct smartfs_file_s *priv;

  /* Recover our private data from the struct file instance */

  priv = (FAR struct smartfs_file_s *)filep->f_priv;
  DEBUGASSERT(priv);

  /* Release the file attributes structure */

  kmm_free(priv);
  filep->f_priv = NULL;
  return OK;
}

/****************************************************************************
 * Name: smartfs_read
 ****************************************************************************/

static ssize_t smartfs_read(FAR struct file *filep, FAR char *buffer,
                           size_t buflen)
{
  FAR struct smartfs_file_s *priv;
  ssize_t ret;

  fvdbg("buffer=%p buflen=%d\n", buffer, (int)buflen);

  /* Recover our private data from the struct file instance */

  priv = (FAR struct smartfs_file_s *)filep->f_priv;
  DEBUGASSERT(priv);

  /* Perform the read based on the directory entry */

  ret = 0;

  if (priv->level1.base.level == 3)
    {
      if (priv->level1.direntry < g_direntrycount)
        {
          ret = g_direntry[priv->level1.direntry].read(filep, buffer, buflen);
        }
    }

  /* Update the file offset */

  if (ret > 0)
    {
      filep->f_pos += ret;
    }

  return ret;
}

/****************************************************************************
 * Name: smartfs_dup
 *
 * Description:
 *   Duplicate open file data in the new file structure.
 *
 ****************************************************************************/

static int smartfs_dup(FAR const struct file *oldp, FAR struct file *newp)
{
  FAR struct smartfs_file_s *oldpriv;
  FAR struct smartfs_file_s *newpriv;

  fvdbg("Dup %p->%p\n", oldp, newp);

  /* Recover our private data from the old struct file instance */

  oldpriv = (FAR struct smartfs_file_s *)oldp->f_priv;
  DEBUGASSERT(oldpriv);

  /* Allocate a new container to hold the task and attribute selection */

  newpriv = (FAR struct smartfs_file_s *)kmm_malloc(sizeof(struct smartfs_file_s));
  if (!newpriv)
    {
      fdbg("ERROR: Failed to allocate file attributes\n");
      return -ENOMEM;
    }

  /* The copy the file attribtes from the old attributes to the new */

  memcpy(newpriv, oldpriv, sizeof(struct smartfs_file_s));

  /* Save the new attributes in the new file structure */

  newp->f_priv = (FAR void *)newpriv;
  return OK;
}

/****************************************************************************
 * Name: smartfs_opendir
 *
 * Description:
 *   Open a directory for read access
 *
 ****************************************************************************/

static int smartfs_opendir(FAR const char *relpath, FAR struct fs_dirent_s *dir)
{
  FAR struct smartfs_level1_s *level1;
  int        ret;

  fvdbg("relpath: \"%s\"\n", relpath ? relpath : "NULL");
  DEBUGASSERT(relpath && dir && !dir->u.procfs);

  /* The path refers to the 1st level subdirectory.  Allocate the level1
   * dirent structure.
   */

  level1 = (FAR struct smartfs_level1_s *)
     kmm_malloc(sizeof(struct smartfs_level1_s));

  if (!level1)
    {
      fdbg("ERROR: Failed to allocate the level1 directory structure\n");
      return -ENOMEM;
    }

  /* Initialize base structure components */

  ret = smartfs_find_dirref(relpath, level1);

  if (ret == OK)
    {
      dir->u.procfs = (FAR void *) level1;
    }
  else
    {
      kmm_free(level1);
    }

  return ret;
}

/****************************************************************************
 * Name: smartfs_closedir
 *
 * Description: Close the directory listing
 *
 ****************************************************************************/

static int smartfs_closedir(FAR struct fs_dirent_s *dir)
{
  FAR struct smartfs_level1_s *priv;

  DEBUGASSERT(dir && dir->u.procfs);
  priv = dir->u.procfs;

  if (priv)
    {
      kmm_free(priv);
    }

  dir->u.procfs = NULL;
  return OK;
}

/****************************************************************************
 * Name: smartfs_readdir
 *
 * Description: Read the next directory entry
 *
 ****************************************************************************/

static int smartfs_readdir(struct fs_dirent_s *dir)
{
  FAR struct smartfs_level1_s *level1;
  int ret, index;

  DEBUGASSERT(dir && dir->u.procfs);
  level1 = dir->u.procfs;

  /* Have we reached the end of the directory */

  index = level1->base.index;
  if (index >= level1->base.nentries)
    {
      /* We signal the end of the directory by returning the special
       * error -ENOENT
       */

      fvdbg("Entry %d: End of directory\n", index);
      ret = -ENOENT;
    }

  /* We are traversing a subdirectory of task attributes */

  else
    {
      DEBUGASSERT(level1->base.level >= 1);

      /* Test the type of directory listing */

      if (level1->base.level == 1)
        {
          /* Listing the top level (mounted smartfs volumes) */

          if (!level1->mount)
            {
              return -ENOENT;
            }

          dir->fd_dir.d_type = DTYPE_DIRECTORY;
          strncpy(dir->fd_dir.d_name, level1->mount->fs_blkdriver->i_name, NAME_MAX+1);

          /* Advance to next entry */

          level1->base.index++;
          level1->mount = level1->mount->fs_next;
        }
      else if (level1->base.level == 2)
        {
          /* Listing the contents of a specific mount */

          dir->fd_dir.d_type = g_direntry[level1->base.index].type;
          strncpy(dir->fd_dir.d_name, g_direntry[level1->base.index++].name, NAME_MAX+1);
        }
      else if (level1->base.level == 3)
        {
          /* Listing the contents of a specific entry */

          dir->fd_dir.d_type = g_direntry[level1->base.index].type;
          strncpy(dir->fd_dir.d_name, g_direntry[level1->direntry].name, NAME_MAX+1);
          level1->base.index++;
        }

      /* Set up the next directory entry offset.  NOTE that we could use the
       * standard f_pos instead of our own private index.
       */

      ret = OK;
    }

  return ret;
}

/****************************************************************************
 * Name: smartfs_rewindir
 *
 * Description: Reset directory read to the first entry
 *
 ****************************************************************************/

static int smartfs_rewinddir(struct fs_dirent_s *dir)
{
  FAR struct smartfs_level1_s *priv;

  DEBUGASSERT(dir && dir->u.procfs);
  priv = dir->u.procfs;

  priv->base.index = 0;
  return OK;
}

/****************************************************************************
 * Name: smartfs_stat
 *
 * Description: Return information about a file or directory
 *
 ****************************************************************************/

static int smartfs_stat(const char *relpath, struct stat *buf)
{
  int ret;
  struct smartfs_level1_s level1;

  /* Decide if the relpath is valid and if it is a file
   *        or a directory and set it's permissions.
   */

  ret = smartfs_find_dirref(relpath, &level1);

  buf->st_mode = S_IROTH|S_IRGRP|S_IRUSR;
  if (ret == OK)
    {
      if (level1.base.level < 3)
        {
          buf->st_mode |= S_IFDIR;
        }
      else
        {
          if (g_direntry[level1.direntry].type == DTYPE_DIRECTORY)
            {
              buf->st_mode |= S_IFDIR;
            }
          else
            {
              buf->st_mode |= S_IFREG;
            }
        }
    }

  /* File/directory size, access block size */

  buf->st_size    = 0;
  buf->st_blksize = 0;
  buf->st_blocks  = 0;

  return ret;
}

/****************************************************************************
 * Name: smartfs_status_read
 *
 * Description: Performs the read operation for the "status" dir entry.
 *
 ****************************************************************************/

static size_t smartfs_status_read(FAR struct file *filep, FAR char *buffer,
                                  size_t buflen)
{
  struct mtd_smart_procfs_data_s procfs_data;
  FAR struct smartfs_file_s *priv;
  int       ret;
  size_t    len;
  int       utilization;

  priv = (FAR struct smartfs_file_s *) filep->f_priv;


  /* Initialize the read length to zero and test if we are at the
   * end of the file (i.e. already read the data.
   */

  len = 0;
  if (priv->offset == 0)
    {
      /* Get the ProcFS data from the block driver */

      ret = priv->level1.mount->fs_blkdriver->u.i_bops->ioctl(
          priv->level1.mount->fs_blkdriver, BIOC_GETPROCFSD,
          (unsigned long) &procfs_data);

      if (ret == OK)
        {
          /* Calculate the sector utilization percentage */

          if (procfs_data.blockerases == 0)
            {
              utilization = 100;
            }
          else
            {
              utilization = 100 * (procfs_data.blockerases * procfs_data.sectorsperblk -
                procfs_data.unusedsectors) / (procfs_data.blockerases *
                procfs_data.sectorsperblk);
            }

          /* Format and return data in the buffer */

          len = snprintf(buffer, buflen, "Format version:    %d\nName Len:          %d\n"
                                         "Total Sectors:     %d\nSector Size:       %d\n"
                                         "Format Sector:     %d\nDir Sector:        %d\n"
                                         "Free Sectors:      %d\nReleased Sectors:  %d\n"
                                         "Sectors Per Block: %d\n",
                                         //"Unused Sectors:    %d\nBlock Erases:      %d\n"
                                         //"Sectors Per Block: %d\nSector Utilization:%d%%\n",
                  procfs_data.formatversion, procfs_data.namelen,
                  procfs_data.totalsectors, procfs_data.sectorsize,
                  procfs_data.formatsector, procfs_data.dirsector,
                  procfs_data.freesectors, procfs_data.releasesectors,
                  procfs_data.sectorsperblk);
                  //procfs_data.unusedsectors, procfs_data.blockerases,
                  //procfs_data.sectorsperblk, utilization);
        }

      /* Indicate we have already provided all the data */

      priv->offset = 0xFF;
    }

  return len;
}

/****************************************************************************
 * Name: smartfs_mem_read
 *
 * Description: Performs the read operation for the "mem" dir entry.
 *
 ****************************************************************************/

#ifdef CONFIG_MTD_SMART_ALLOC_DEBUG
static size_t   smartfs_mem_read(FAR struct file *filep, FAR char *buffer,
                  size_t buflen)
{
  struct mtd_smart_procfs_data_s procfs_data;
  FAR struct smartfs_file_s *priv;
  int       ret;
  uint16_t  x;
  size_t    len, total;

  priv = (FAR struct smartfs_file_s *) filep->f_priv;


  /* Initialize the read length to zero and test if we are at the
   * end of the file (i.e. already read the data.
   */

  len = 0;
  if (priv->offset == 0)
    {
      /* Get the ProcFS data from the block driver */

      ret = priv->level1.mount->fs_blkdriver->u.i_bops->ioctl(
          priv->level1.mount->fs_blkdriver, BIOC_GETPROCFSD,
          (unsigned long) &procfs_data);

      if (ret == OK)
        {
          /* Print the allocations to the buffer */

          total = 0;
          len = snprintf(buffer, buflen, "Allocations:\n");
          buflen -= len;
          for (x = 0; x < procfs_data.alloccount; x++)
            {
              /* Only print allocations with a non-NULL pointer */

              if (procfs_data.allocs[x].ptr != NULL)
                {
                  len += snprintf(&buffer[len], buflen-len, "   %s: %d\n",
                      procfs_data.allocs[x].name, procfs_data.allocs[x].size);
                  total += procfs_data.allocs[x].size;
                }
            }

          /* Add the total allocation amount to the buffer */

          len += snprintf(&buffer[len], buflen-len, "\nTotal: %d\n", total);
        }

      /* Indicate we have done the read */

      priv->offset = 0xFF;
    }

  return len;
}
#endif

/****************************************************************************
 * Name: smartfs_erasemap_read
 *
 * Description: Performs the read operation for the "erase" dir entry.
 *
 ****************************************************************************/

#ifdef CONFIG_MTD_SMART_SECTOR_ERASE_DEBUG
static size_t   smartfs_erasemap_read(FAR struct file *filep, FAR char *buffer,
                  size_t buflen)
{
  struct mtd_smart_procfs_data_s procfs_data;
  FAR struct smartfs_file_s *priv;
  int       ret, rows, cols;
  size_t    x, y;
  size_t    len, copylen;

  priv = (FAR struct smartfs_file_s *) filep->f_priv;

  /* Get the ProcFS data from the block driver */

  ret = priv->level1.mount->fs_blkdriver->u.i_bops->ioctl(
      priv->level1.mount->fs_blkdriver, BIOC_GETPROCFSD,
      (unsigned long) &procfs_data);
  if (ret != OK)
    {
      return 0;
    }

  /* Initialize the read length to zero and test if we are at the
   * end of the file (i.e. already read the data).
   */

  len = 0;
  rows = 32;
  cols = procfs_data.neraseblocks / rows;
  while (rows >= 4 && (cols < 64 || cols > 128))
    {
      rows >>= 1;
      cols = procfs_data.neraseblocks / rows;
    }

  /* Continue sending data until everything sent.  We add 'rows' below to
   * account for the \n at the end of each line.
   */

  if (priv->offset < procfs_data.neraseblocks + rows)
    {
      /* copylen keeps track of the current length.  When it is
       * equal to or greater than the offset, we start sending data
       * again.  Basically we are starting at the beginning each time
       * and only sending where we left off and discarding the rest.
       */

      copylen = 0;
      for (y = 0; y < rows; y++)
        {
          //for (x = 0; x < 128; x++)
          for (x = 0; x < cols; x++)
            {
              /* Copy data to the buffer */

              if (copylen >= priv->offset)
                {
                  buffer[len++] = procfs_data.erasecounts[y*cols+x] + 'A';
                  priv->offset++;

                  if (len >= buflen)
                    return len;
                }
              copylen++;
            }

          /* Add a trailing \n */

          if (copylen >= priv->offset)
            {
              buffer[len++] = '\n';
              priv->offset++;
              if (len >= buflen)
                return len;
            }

          /* Terminate the string */

          if (copylen >= priv->offset)
            {
              buffer[len++] = '\0';
              priv->offset++;
            }

          copylen++;
        }
    }

  return len;
}
#endif

/****************************************************************************
 * Public Functions
 ****************************************************************************/

#endif /* CONFIG_FS_PROCFS && !CONFIG_FS_PROCFS_EXCLUDE_SMARTFS */